Call an event within an event
Assume that I have two buttons: Button1 and Button2. Assume that I have the code in a Button1_Click defined. How call (run) the Button1_Click event within the Button2_Click event code?
PrivateSub Button2_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button2.ClickButton1.Click()
EndSubPrivateSub Button1_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button1.Click' code does something ...
EndSub
[1646 byte] By [
twaltz] at [2007-12-24]
Simply call the method, one does not have to fire an event:
Button1_Click(nothing, nothing)
Or you can pass in the parameters sent in to the button2, instead of nothing if they are needed in button1.
Two options:
1) If you want to pretend that the button was clicked (which will call *all* event handlers for the button's event handler - there is nothing that prevents you from having multiple handlers), you can use the Button's PerformClick method
2) If you want to do one specific thing that is also done when button2 is called, I would do what OmeageMan suggests with one little addition; instead of putting all of your logic in the Button2_Click event, I would break it out to a new method that has a more descriptive name and call this method from both Button1 and Button2's event handlers; for example if Button1 was to "enable all controls" (replace with a descriptive name of what button1 actually is supposed to do), it'd look like this:
Private Sub Button1_Click(sender as Object, e as eventargs) Handles Button1.Click
EnableAllControls()
End Sub
Private Sub Button2_Click(sender as Object, e as eventargs) Handles Button2.Click
....
EnableAllControls()
End Sub
Private Sub EnableAllControls()
....
End Sub
Best regards,
Johan Stenberg
Why not just use the same event?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click