Henry VB.NET Tour (Fourteen) - Dynamic Association Events and Procedures

zhaozj2021-02-16  63

??? Henry's VB.NET Tour (14) - Dynamic Association Events and Procedures

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

?

"To explain the new event handler method, we need to say a few important questions first." Da Li began to stand serious, I have to move the body and show the grinding scene.

"We first discuss how the event is generated. The event is the message sent by the object, with the occurrence of signal notification operation. Operation may be interactive by the user, such as the mouse click, or may be managed by some other program logic Triggering. The object of the incident is called the event sender (ah, hearing this, Henry suddenly understood why the first parameter in the event handler is called Sender, means the object sent by the event). Capture the event and pair The object of responding is called an event receiver. In the event communication, the event sender class does not know which object or method will receive the event it triggered. The required is a medium between the source and the receiver, or similar The mechanism of the pointer .. The NNET framework defines a special type of Delegate, which is delegated, which provides functions of the function pointer. "

"Ah, I have long heard of the delegate, I don't know what it means!" I made a look.

"The entrustment is an object that can be used to call other object methods. Unlike other classes, the delegate class has a signature, and it can only be referenced to the method that matches its signature." Da Li continued.

"Hey, the entrustment is also a function pointer." "I seem to understand it.

"Almost, the commission can be equivalent to a type of security function pointer or a callback. But different from the function pointer, Visual Basic.Net delegate is a reference type of the System.Delegate class, which can reference our previous sharing method (details See "Sharing member" one) and instance method. "

"I understand." I said on the side, "We want to dynamically call the event handler, is it necessary to use the delegation to declare which program is used to handle events?"

Da Li looked at me amazed, showing a bit of praise.

"Entrusted is an important type in .NET, we need to discuss in detail later. Now we need to pay attention to how to connect events and event handler dynamically through operational entrustment." Da Li then start modifying code. :

?

Module Module1

Public Class Chenry, PUBLIC CLASS CHENRY

??????? public evenet er () ??? 'declared an event

??????? Sub causesomeevent ()

??????????? raiseelent eventhr ()? 'Triggered an event

??????? End Sub

??? End Class

?

??? Dim obj asse new chenry ()

??? Sub obj_eventhr ()? Declared event after Handles

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

??? End Sub

??? Sub main ()

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

??????? Obj.causesomeevent () ?? 'call the object to trigger the event

End Sub

End module

?

"Is there something different?" Da Li turned to ask me.

"There are two main two, first, the definition of OBJ is no longer identified by WitHevents, so the event handler OBJ_EVENTHR () will not declare events through the Handles keyword, that is, EventHR events and event handler do not use WitHevent -Handles is associated; secondly, addHandle and Addressof ... "I said this, I have no words, I can only speak. "Oh, let me help you say." Da Li took my shoulder, then said, "first said Addressof, the Addressof operator created a process delegation to the designated process. We just said that the delegation is equivalent to A function pointer, then addressof is the entrusted operator, and it can be delegated by it. "

Seeing me later, Da Li and then said: "Light looks that addhandle can associate Obj.Eventhr events with the obj_eventhr event handler, you will definitely not understand where what I said, because I didn't Refer to another method REMOVEHANDLER. Its use method is the same, such as:

REMOVEHANDLER OBJ.EVENTHR, Addressof Obj_Eventhr

You see, addHandler and RemoveHandler can provide greater flexibility than the Handles clause, as long as we are good at using them, you can dynamically add, remove, and change the event handler associated with an event. And more powerful than Handles is that AddHandler allows multiple event handles to be associated with a single event. "

Big Li stopped a stop, then said: "What you have to pay attention to is that the entrusted signature followed by Addressof should be consistent with the corresponding event data class, let's take an example."

?

AddHandler TextBox.MouseDown, Addressof TextBoxMousedownHandler

'Wrong example 1:

Private sub textboxmousedownhandler ()

End Sub

'错 误 示 2

Private sub textboxmousedownhandler (Byval E AS Object, Byval E as Eventargs)

End Sub

'Correct example:

Private Sub TextBoxMousedownHandler (Byval e as mouseeventargs)

End Sub

?

? "The event is a mouse in a text box. We don't have to own the standard associated event handling method, and use AddHandler to implement, the parameters of the corresponding method after addressof, should be commissioned with the event corresponding to the mousedown event MouseEventHandler has the same signature, which is to be consistent on the parameter declaration, an Object variable, a system.windows.forms.mouseeventargs variable.

I have understood this step, and the event handler can dynamically establish or disconnect the relationship between the event and the event handler through the AddHandler and the RemoveHandler method when we need. However, I am still not understood from the "commissioned mouseeeventhandler" that Da Li said.

?

(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/ilatitude/

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

New Post(0)