In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of VB.NET dynamic events, the article is very detailed, has a certain reference value, interested friends must read it!
"Brother Li," I can't help but feel embarrassed that I have asked so many questions. "what is the commission of the event you just mentioned?"
Big Li obviously understood that I would ask: "do you remember what I said about the three elements of event-driven design?"
"of course, it means objects, events, and event handlers." I answered fluently.
"We have analyzed the objects and event handlers. To thoroughly understand the context of VB.NET dynamic event drivers, we must understand the composition of events." Seeing me shake my head blankly again, he added, "the event function is provided by three interrelated elements: the class that provides the event data, the event delegate, and the class that raises the event." As I said, an event is a signal that tells the application that something important is about to happen. Then we can imagine that if an event is to occur, it will contain unique information, such as what the source of the event is and what kind of event will happen. The class that provides event data is used to record this information.
This class must be derived from System.EventArgs, which we have already said; the delegate of the event, which I have also said, is like a pointer to the recipient of the event. Because the event of the specified object is unique, its delegate is also pre-defined, such as the MouseDown event, and its corresponding delegate is MouseEventHandler. Let's see if you select the MouseDown method of the Label1 object in the code window, this empty event handler will be automatically generated:
Friend WithEvents Label1 As
System.Windows.Forms.Label
Private Sub Label1_MouseDown
(ByVal sender As Object, ByVal e As _
System.Windows.Forms.Mouse
EventArgs) Handles Label1.MouseDown
End Sub
It's actually the same as:
AddHandler Label1.MouseDown
AddressOf Label1_MouseDown
Private Sub Label1_MouseDown
(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs)
End Sub
The implication is to implement the definition of event handler parameters through the VB.NET dynamic event delegate MouseEventHandler that has been previously declared as a global variable. For MouseDown events, why the automatically added event data class parameters are MouseEventArgs is the role of the delegate.
The third element is the class that raises the event. This class must provide the declaration of the event and the method that raises the event. The method that raises the event must be named On plus the name of the event. For example, if the event is EventHR, then the method that raises the event must be called OnEventHR. "
As he spoke, Da Li began to modify the code that had just been used for standard event handlers:
Module Module1
'event data class
Public Class HenryEventArgs
Inherits EventArgs
End Class
'declare delegate
Public Delegate Sub HenryEvent
Handler (ByVal sender As Object, _
ByVal e As HenryEventArgs)
Public Class CHenry
'declare an event
Public Event EventHR As
HenryEventHandler
Protected Overridable Sub
OnEventHR (ByVal e As HenryEventArgs)
'invoke the delegate
RaiseEvent EventHR (Me, e)
End Sub
Public Sub start ()
Dim e As HenryEventArgs
OnEventHR (e)
End Sub
End Class
Dim obj As New CHenry ()
Sub obj_EventHR (ByVal
Sender As Object, ByVal
E As HenryEventArgs)
MsgBox ("the event handler captured the
Event. () handle events.
End Sub
Sub Main ()
AddHandler obj.EventHR
AddressOf obj_EventHR
Obj.start ()
End Sub
End Module
Da Li pointed to the written code and said to me, "you see, this code is quite different from the previous one. First of all, the EventHR event I define as a delegate type, is no longer a bare VB.NET dynamic event. In this way, the EventHR event has the ability to carry event information, and the event source is the object of the Sender; event data is the HenryEventArgs class. You should be able to understand everything else very quickly, right?"
"well, there is another question, why does OnEventHR, the method that raises the event, use the protection method?" I'm still a little confused.
"that's a good question!" Dali nodded. "this is so that the derived class must always call the OnEventHR method of the base class to ensure that the registered delegate receives the event. You only have to try and inherit CHenry to get a derived class for event handling, and you will understand."
The above is all the content of the article "sample Analysis of VB.NET dynamic events". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.