Henry's Journey to the VB.net (13)-standard event handlers

Source: Internet
Author: User
Standard | Procedure Henry's vb.net tour (13)-standard event handlers

Han Rui



"Interest is good!" Big Li nodded slightly and then asked me, "Do you know how the program knows which objects can cause events, and how to correlate events and event handlers?" ”

See my lost eyes, Big Lee with the mouse click to open the "Windows Forms Designer generated code" by region, pointing to the middle of a sentence automatically generated code:

Friend WithEvents Button1 as System.Windows.Forms.Button




"When you add a button to a form in the design window, the code is automatically added to the code, and the Friend keyword grants friend access to one or more of the declared programming elements." So, what's withevent? ”

I immediately looked for help, with:

The WithEvents keyword indicates that the declared object variable references the class instance that can raise the event.

"See," I can't always be silly. "When you declare an object, use WithEvents to identify it, and then using the Handles keyword at the end of the procedure declaration in the event handler will be able to handle the event that is raised by the object variable declared using the WithEvents keyword. ”

"Yes, you see I write a piece of code, using the Withevent-handles method to handle the event." Class name is Chenry bar, it contains an event eventhr. ”



Module Module1

Public Class Chenry

Public event Eventhr () ' Declares an event

Sub causesomeevent ()

RaiseEvent Eventhr () ' raises an event

End Sub

End Class



WithEvents OBJ as New chenry () ' Module or class-level declaration

Sub obj_eventhr () Handles Obj.eventhr ' declare event after Handles

MsgBox ("event handler captures event.") ' Handle the event.

End Sub

Sub Main ()

Obj.causesomeevent () ' invokes an object to raise an event

End Sub

End Module




"What you need to notice is how to raise an event through RaiseEvent, and another very simple question to note is that the name of the event handler is usually the" object name _ event name "approach. Big Li Side writes the program side to say.

"is the declaration and throw of the event only within one level, or can the base class's events be raised in a derived class?" "I am not curious to ask.

"You start to learn to think," Big Li don't know is not laughing at me, "VB. NET requirements must raise events within the scope of the Declaration event. A derived class cannot raise an event that inherits from a base class, but can handle events raised by the base class. We can take a look at an example. "Big Lee immediately modified the code that was just written:



Module Module1

Public Class Chenry

Public event Eventhr () ' Declares an event

Sub causesomeevent ()

RaiseEvent Eventhr () ' raises an event

End Sub

End Class

public class Class2 ' class derived from Chenry

Inherits Chenry

Sub obj_eventhr () Handles mybase.eventhr

MsgBox ("event handler captures event.") ' Handle the event.

End Sub

End Class



WithEvents OBJ as New Class2 () ' Module or class-level declaration

Sub Main ()

Obj.causesomeevent () ' invokes an object to raise an event

End Sub

End Module




"Remember what I told you about MyBase?" (Note: See "Structure and deconstruction") we can add Handles mybase.<event name> statements to declare the event handlers in a derived class. To ask a basic question, you see that obj is an instance of the Class2 class, right? But why does it also have the Causesomeevent method of Chenry class? "Big Lee said my eyeballs are going to fall."

"Of course because Class2 is a derived class of the Chenry class, it inherits its method." "I look extremely depressed.

"Haha, don't worry, in the vb.net we most often encounter is the object-oriented problem." Keep looking back on the good, warm and know new! "Big Lee's last smile is a piece of advice to me." To learn vb.net well, I really need to review the object-oriented content.

Big Lee Topic A turn, lightly to say to me: "The WithEvents statement and the HANDLES clause provide a standard declarative method for specifying event handlers. That is, how to associate an object's event with an event handler. An event raised by an object declared by WithEvents can be handled by any procedure with the Handles clause that names the event. In other words, an event handler that is identified by a handles clause can only handle objects declared by WithEvents. Although the Handles clause is the standard method for associating events with event handlers, it is limited to associating events and event handlers at compile time. There is also a way to allow the event to be dynamically connected or disconnected at run time with one or more event handlers, rather than requiring the use of WithEvents to declare an object variable. ”

"Is it?" I suddenly awakened from meditation, and the curiosity struck again.



(To be continued)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.