How to call an Event

Joined
Jan 15, 2007
Messages
10
Programming Experience
Beginner
I have a problem with calling an event i dont know how i can go about this. here is it is. Hope someone could help me out.

I need to call a controls event from another event. I can't figure out how. example...

Private Sub btn1_Click(....) handles btn1.click
.....
End Sub.

I want to call the above event thru another button's event..

Private sub btn2_Click(...) handles btn2.click
.... btn1_Click event here... but how?
End Sub

I was checking on my MSDN included in the .Net help but all I can find was the RaiseEvent and Addhandler/RemoveHandler but I really dont know how to use the code..

Please help thanks.
 
Use the System.Windows.Forms.Button.PerformClick() method which generates a System.Windows.Forms.Control.Click event for a button:
VB.NET:
Private sub btn2_Click(...) handles btn2.click
    btn1.PerformClick()
End Sub
 
PerformClick goes for Button click event, else the regular way is just to define the Sub method and call it from all places you need to. In some cases you may also use the same actual event handler for several controls event.

Example pseudo code call a method:
VB.NET:
Private sub btn2_Click(...) handles btn2.click
    update()
End Sub
 
Private sub grid_focus(...) handles grid.focus
    update()
End Sub
 
sub update()
  'does update
end sub
Example pseudo code multiple handlers:
VB.NET:
Private sub btn2_Click(...) handles btn1.click, btn2.click, btn3.click
    'updates
End Sub
 
Got it, Thank you very much.

How about the RaiseEvent how do you use it? also the Addhandler and RemoveHandler methods? Those are the commands I found when I searched the MSDN on how to call events.

Private sub Btn1_Click(...)handles btn1.click
AddHandler btn1.click, Addressof btn2.click

End Sub

Private Sub btn2_Click(....)handles btn2.click
Update...
End Sub

I tried it but it doesn't seem to work, same with the RaiseEvent EventName().

I wonder why?
 
Last edited:
Addhandler/RemoveHandler adds or removes a handler method of an event, it is the manual equivalent of the Handles clause, and enables you to dynamically work with event handlers in code.

RaiseEvent is used internally in a class to raise one of its events. For example given some condition the Button class raises its Click event. If you subscribe to this event your handler method is executed.
 
I understand the Addhandler/removehandler method,
but the RaiseEvent is not quite clear...
can you give an example of a RaiseEvent? and how you subscribe to an event.

I really appreaciate your help.
 
VB.NET:
Friend Event Failed(ByVal Reason As String)

RaiseEvent Failed("I/Q level is not high enough to complete the task")

is an example of declaring your own event then raising it so whatever class called it can handle it (if the event is coded in the other class that uses your class)
 
Back
Top