Henry's VB.NET Tour (13) - Standard Event Processing

zhaozj2021-02-16  56

???? Henry's VB.NET Tour (13) - Standard Event Processing

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

?

"Interested!" Da Li nodded, then asked me, "How do you know how to know what objects can cause, and how to associate events and event handroots?"

Seeing my confused eyes, Dafu uses the mouse click to open the "Windows Form Designer generated by the REGON", "pointing at the intermediate, automatically generated code:

??? Friend WitHevents Button1 As System.Windows.Forms.Button

?

"When you add a button on the design window to the form, you will automatically add this code in your code, and the Friend keyword grants friend access to one or more declarative programming elements. So what WitHevent is doing? of?"

I immediately found in my help, there is:

The WitHevents keyword indicates that the declared object variable reference can trigger the class instance of the event.

"Understand," I can't always say, "" With WitHevents when you declare the object, you can use the Handles keyword to use the Handles keyword at the end of the process declaration in the event handler. The events caused by object variables. "

"Yes, you seem to write a code, use the WitHevent-Handles method to handle the event. Classified CHENRY, with an event Eventhr."

?

Module Module1

???? public class chenry

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

??????? Sub causesomeevent ()

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

??????? End Sub

??? End Class

?

??? witHevents obj as new chenry () ?? 'Declaration of module or class level

??? Sub obj_eventhr () Handles Obj.Eventhr 'Declared event after Handles

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

??? End Sub

??? Sub main ()

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

??? End Sub

End module

?

"You have to pay attention to how to trigger events through raiseEvent, and another simple question needs to be noted that the naming of the event handler is generally using the 'object name _ event name' method." Da Li Edward pointer.

"The statement of the incident and causes only an event that can only trigger the base class in the derived class in the derived class?" I couldn't help but ask.

"You start to learn to think," Da Li didn't know if it was laughing at me. Event. We can come to see an example. "Da Li immediately revised the code written on:

?

Module Module1

???? public class chenry

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

??????? Sub causesomeevent ()

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

??????? End Sub

??? End Class

??? public class class2 'class from chenry ??????? inherits chenry

??????? Sub obj_eventhr () handles mybase.eventhr

??????????? msgbox ("Event Processor captures the event.") ")?" Handling the event.

??????? End Sub

??? End Class

?

??? withevents obj asse new class2 () ?? 'declaration of module or class level

??? Sub main ()

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

? ?? End Sub

End module

?

"Remember that I have said mybase with you? (Note: See" Construction and Destructure "One) We can add Handles mybase.

The statement declares the event handler in the derived class. Ask a basic question, do you see an instance of the Class2 class? But why does it also have a Causeesomeevent method with CHENRY class? "Da Li said that my eyes fell.

"Of course, because Class2 is the derived class of the chenry class, inheriting its method." My look is extremely frustrated.

"Haha, don't worry, in VB.NET, we are most often touched to object-oriented problems. Continue to look back, it is good, and you know the new!" Big Li's last smile is not a prior. To learn VB.NET, I really have to review the contents of the object.

Large Li topic turned, faintly told me: "The WitHHevents statement and the handles clause provide a standard statement to specify the event handler. That is, how to associate an object's event and an event handler .withevents The event that the statement of the object can be processed by any process with a Handles clause named this event. In other words, the event handler with the Handles clause identification can only handle the object of the WitHevents declaration. Although the Handles clause is an associated event The standard method with the event handler, it is limited to the compiled event and event handler. There is also a method to allow events to connect or disconnect events during runtime, and Do not require use of WitHevents to declare object variables. "

"Yes?" I woke up from Pens, curiosity and again.

?

(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-25523.html

New Post(0)