Why not work?

Y

YXQ

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MessageBox.Show("ssss")
End Sub

Private Sub eee()
AddHandler Button1.Click, AddressOf Button1_Click
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
eee()
End Sub
*******************************************
Why the codes will not work? Thank you!
 
R

Richard Myers

YXQ said:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MessageBox.Show("ssss")
End Sub

Private Sub eee()
AddHandler Button1.Click, AddressOf Button1_Click
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
eee()
End Sub
*******************************************
Why the codes will not work? Thank you!

Theres nothing wrong with the code. But if you are expecting a message box to be displayed upon
clicking button 2 then you misunderstand the
AddHandler event hook up.

hth
Richard
 
C

Cor Ligthert

YXQ,

It is easier to make this code like this

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click, Button2.Click
MessageBox.Show("ssss")
End Sub
Why the codes will not work? Thank you!

As Richard said, your code works, however it does nothing.

Cor
 
J

Jay B. Harlow [MVP - Outlook]

YXQ,
In addition to the other comments, if you are expecting the message box to
be displayed when eee is called. Then you need to use Button1.PerformClick
instead of AddHandler.

AddHandler adds an event handler to the list of event handlers, it does not
invoke said handler.
Private Sub eee()
Button1.PerformClick
End Sub

NOTE: Only selected controls expose methods such as PerformClick that you
can call to have their events raised.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top