Why not work?

  • Thread starter Thread starter YXQ
  • Start date Start date
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!
 
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
 
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
 
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
 
Back
Top