Sender, E, and handles in VB. NET

Source: Internet
Author: User

In the charging system, the events in are summarized because the parameters in the calls are not very understandable.

In VB6.0, the event is very simple. For the most commonly used click button event, the code in VB is:

Private sub commandtingclick () msgbox ("You clicked button1") end sub

The click event that calls command1 is also simple:

Private Sub Command2_Click() Command1_ClickEnd Sub

In VB6.0, the call event is called Based on the event name and does not require parameters.

However, in, parameters are added to the event:

Private sub button#click (byval sender as system. Object, byval e as system. eventargs) handles msgbox ("You clicked button1") end sub

The event contains three additional information: sender, E, and handles. The following example uses a click in button1 to describe the meaning of the three parameters:

    Private Sub But_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        MsgBox(sender.ToString)        MsgBox(e.ToString)    End Sub

Note that the original button_click is changed to but_click, which is not executed in VB6.0, but does not affect event execution in, which means that in, the event no longer depends on the event name. Let's take a look at the execution result:

The sender type is object type. The displayed result shows that the form button directly causes the event, and the text on the button is "button1 ". The E type is eventargs. The type is interpreted as: eventargs is a delegate declaration, which is the base class of the class containing event data, used to transmit event details. The second display description is: the mouse is used to provide data for this event.

In my understanding, handles is a delegate, which is equivalent to indirectly triggering an event by the parameters following handles. Next I will explain my understanding of the execution process of clicking the button1 event: when you click button1, The button1 next to the handler indirectly triggers the event. The direct sender of the event is the button1 button, that is, the sender. Then, the mouse passes the parameters required to execute the event to the event e, then the code is executed. Take the click event of button1 and button2 as an example:

Private sub but_click (byval sender as system. object, byval e as system. eventargs) handles msgbox ("You clicked button1") end sub private sub butt_click (byval sender as system. object, byval e as system. eventargs) handles, msgbox ("You clicked button2") msgbox (sender. tostring) 'addhandler, addressof button3_click end sub

Click the result of button1:

This indicates that the click event of button2 is added when you click button1. You need to note that such a code method will first execute the code of button2 and then execute the code of button1.

        AddHandler Button1.Click, AddressOf Button3_Click        RemoveHandler Button1.Click, AddressOf Button3_Click

The two lines of code are easy to understand. It must be noted that the first parameter after addhandler and removehandler is "delegate who executes the called event ", the second parameter is the "event to be executed". With these two lines of code, it is easy to delegate or cancel the event in a third-party event.

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: 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.