Hello Friend,
I am new in VB.net,
Can anybody knows that How to use event handlers in vb.net?
Any help will appreciated, :rolleyes:
Thanks,
Printable View
Hello Friend,
I am new in VB.net,
Can anybody knows that How to use event handlers in vb.net?
Any help will appreciated, :rolleyes:
Thanks,
.NET lets you add and remove Event Handlers dynamically on the fly. Your code can start and stop handling events at any time during program execution. Also, you can tie the same code (event handler) to multiple events similar to the Handles clause in VB.NET.
The VB.NET AddHandler and RemoveHandler statements allow this behavior. Both statements take two arguments: the name of the event to handle and the name of the procedure that will handle the event.
Events in the .NET Framework are based on the delegate model. Delegates are type-safe Function Pointers or Callbacks. A delegate can reference both static and instance methods.
Implementation of an event is a three-step procedure.
- Declare a delegate, if definition is not provided the .Net Framework would provide a default delegate implementation.
- Declare the event signature using the Event keyword and Raise the event using the RaiseEvent statement.
- Handle the Event by declaring an event receiver, often called an event handler. Which is a subroutine that responds to an event.
One of the most useful capabilities of the OOP Languages is their inbuilt ability to be aware of a large number of events like MouseOver, MouseClick, and so on so that we can write codes to react to any event that we are interested. This is made possible by the rich set of classes that have been built in the .NET Framework
Some example of event handling using Handles clause are,
Code:Dim withEvents AnEvent as new EventRaised()
Sub EventEgs()
AnEvent.RaiseEvents()
End Sub
Sub AnEvent_EventHandler() Handles AnEvent.EventOne, AnEvent.EventTwo
MsgBox(“Received Event”)
End Sub
Class EventRaised
Public Event EventOne()
Public Event EventTwo()
Sub RaiseEvents()
RaiseEvent EventOne()
RaiseEvent EventTwo()
End Sub
End Class