Henry VB.NET Tour (fifteen) - Dynamic Event Treatment Method

zhaozj2021-02-16  52

??????? Henry's VB.NET Tour (fifteen) - Dynamic event processing method?

?????????????????????????????? Han Rui

?

"Big Li Ge," I can't help you asked so much questions and embarrassed. "What is the entrustment of the incident you mentioned?"

Big Li obviously understands what I will ask: "I still remember the three elements in the event drive design?"

"Of course, it means objects, events, and event handlers." I replied fluently.

"Objects and event handles we have already analyzed. To thoroughly understand the incident of the Event Driver, we must understand the composition of the event." Seeing me once again, he said, "The event function is from three Provided by each other: providing an event data class, an event delegate and a class. I said, the event is a signal, it tells the application to have important cases. So we can imagine it, the event will happen It will contain unique information. For example, what kind of event will happen. 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 said After the incident, this is actually said that it is said that it is a pointer to the event receiver. Since the specified object is unique, its delegation is also defined in advance, such as a mousedown event, It corresponds to the mouseeventhandler. Let's take a look at the mousedown method you choose Label1 object in the code window, which will automatically generate this empty event handler:

?

Friend Withevents Label1 As System.Windows.Forms.Label

Private sub label1_mousedown (byvale AS Object, ByVal E AS _

System.windows.Forms.MouseEventArgs Handles label1.mousedown

End Sub

?

It is actually equivalent to:

?

AddHandler Label1.MouseDown, Addressof Label1_MouseDown

Private sub label1_mousedown (byvale AS Object, ByVal E AS _

System.windows.Forms.MouseEventArgs)

End Sub

?

Implicit meaning is to implement the definition of the event handler parameter by applying the MouseEventHandler that has previously declared the overall variable. For MouseDown events, why the event data class parameters that are automatically added is the mouseeventargs class, which is the role of the commission.

The third element is a class that triggers events. This class must provide an event declaration and a way to raise an event. Among them, the method of triggering the event must be named ON plus the form of the event name. For example, the event is Eventhr, then the method of triggering the event must be called Oneventhr. "

On the one side, Da Li began to modify the code just used for the standard event handler:

?

Module Module1

'Event data class

??? public class henryEventArgs

??????? inherits Eventargs

??? End Class

??? 'declaration commission

??? public delegate sub HenryEventHandler (Byval Sender As Object, _

?? Byval E As HenryEventArgs

??? public class chenry

??????? 'declared an event ??????? public evenethr as HenryEventHandler

??????? Protected Overridable Sub Oneventhr (ByVal E AS HenryEventArgs)

??????????? 'call delegation

??????????? RaiseEvent EventHR (ME, E)

??????? End Sub

??????? public sub start ()

??????????? Dim e as HenryEventArgs

??????????? Oneventhr (e)

??????? End Sub

??? End Class

??? Dim obj asse new chenry ()

??? Sub obj_eventhr (Byval E AS HenryEventArgs)

??????? msgbox ("The event processor captures the event.") ") ?? 'Handling the event.

??? End Sub

?

??? Sub main ()

??????? addhandler obj.eventhr, addressof obj_eventhr

??????? Obj.start ()

End Sub

End module

?

?

Big Li pointed to the code to say: "You see, this code has a lot of differences from the previous difference. First, EventHR event I defined as a delegate type, no longer a bare event. In this way EventHR events have the ability to carry event information, the event delivery source is Sender; event data is an object of the HenryEventArgs class. Other you should soon understand, right? "

"Well, there is a problem, why do you trigger an event?" What is the way to use protection? "I still have some intake.

"This question is good!" Da Li nodded, "This is to let the derived class must always call the list of oneventhr methods of the base class to ensure that the registered delegate receives the event. You only need to try, inherited chenry to get a derived class The event is handled, it will be understood. "

Oh, an event driver design method tells me to open my eyes, open a big door to understand VB.NET, continue to work hard, I have to come to fuel!

(Endlessly)

-------------------------------------------------- -------------

Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.

QQ: 18349592

E-mail: Henry7685@hotmail.com

Please visit my column: http://www.9cbs.net/develop/author/netauthor/latitude/

转载请注明原文地址:https://www.9cbs.com/read-25521.html

New Post(0)