This.button1.Click + = new System.EventHandler (This.button1_click);

Source: Internet
Author: User

In this program, the change here is to better understand, of course, this is a grammatical error.

Below we analyze it:

First, observe the expression "=" to the right.

New System.EventHandler (This.button1_click);

By the instructions in section 6.1.11, you can see that this code actually establishes an instance of a delegate type and makes the delegate point to the This.button1_click method. That is, the system will invoke the This.button1_click method indirectly through this delegate instance at "a moment" when the program is running.

Then we'll look at the expression "=" to the left. In C-style languages, "=" is an assignment expression, that is, the data type of the expression on both sides of the "=" should be the same. Therefore, since the expression on the right side of "=" is an instance of a delegate type (System.EventHandler), then This.button1.Click should also be a delegate type (System.EventHandler).

Through the above instructions, we get a message, the previous event registration code, is to let This.button1.Click and System.EventHandler (This.button1_click) point to the same memory space, simply speaking, is to let This.button1.Click point to the This.button1_click method, called the This.button1.Click, is equivalent to calling the This.button1_click method. Therefore, we say that when the This.button1 click event occurs, the method This.button1_click is called.

When the program is running, the system will detect if This.button1 is clicked, and if clicked, call Button1.Click inside the button1, then the Button1_Click method in the Windows window will be executed.

Of course, the event registration code can be fully handwritten. Event registration In other classes is handwritten, except that the event registration code in the control is auto-generated. The methods for registering events manually are as follows:

First, you can add events (usually in the window's constructor) to any code before the event, and we'll manually register the Button1 MouseMove event as shown in:

When we finish "=", there will be a prompt "press TAB to insert", at this time, we only need to pressing 2 "tab" key, the event registration and the method used for the callback will be added to the code window itself, as shown in:

The automatically generated code refers to the This.button1 MouseMove event that points to the Button1_mousemove method. This handwriting code is exactly the same as the IDE's auto-generated code.

Of course, as the control event, we can automatically generate, if you want to automatically generate Button1 other events, only need to view the Button1 Properties window, and click the "" button, the control will appear the event list, as shown in:

Then double-click on the event you want, and the code will be generated automatically.

In front of the polygon code in order to better understand the event registration, we have

This.button1.Click + = new System.EventHandler (This.button1_click);

Revision changed to

This.button1.Click = new System.EventHandler (This.button1_click);

We will find that whether you write the event registration code, or automatically generated code, are using "+ =" to achieve, in fact, as the event registration code, we can only use "+ =" to achieve registration, simple use "=" is a syntax error!!!

The "+ =" operator is a commonly used operator in C-style languages, such as

int i=0;

I+=1;

Equivalent to

int i=0;

i=i+1;

So

This.button1.Click + = new System.EventHandler (This.button1_click);

In principle is equivalent to

This.button1.Click = This.button1.Click +

New System.EventHandler (This.button1_click);

Using natural language to describe the above code is "one delegate = the delegate itself + another delegate". So what does the addition of the delegate mean?

In section 6.1.31, we discussed multidelegate (multicast delegation), and the event itself is a delegate, and all delegates are derived classes of the System.multidelegate class, and in 6.1.3 we have demonstrated that multiple delegate type instances are added, which is to store these delegate instances in a multi- In the call chain of a multicast delegate, when a multicast delegate is called, all the delegates in the call chain of that multicast delegation are called sequentially.

Using the principle of multicast delegation, we can register multiple methods to an event, as follows:

This.button1.Click +=new System.EventHandler (This.button1_click);

This.button1.Click +=new System.EventHandler (THIS.BUTTON1_CLICK1);

This.button1.Click +=new System.EventHandler (THIS.BUTTON1_CLICK2);

The above code registers three methods in the Button1 click event, and the method Button1_click,button1_click1,button1_click2 will be called sequentially when Button1 's Click event is triggered. The advantage of this is that we can put multiple functions and logically completely independent operations in different methods, and when events occur, these methods will be called sequentially to achieve the Cascade operations I need.

callback method for events in the 6.2.3 control

Now that we've finished registering the event, let's talk about the callback method for the event. First, let's review the code for event registration again:

This.button1.Click +=new System.EventHandler (This.button1_click);

In the above code, use "New System.EventHandler (This.button1_click)" to point an instance of a System.EventHandler delegate type to the This.button1_click method. As discussed in section 6.1.11, we know that if you want a delegate to point to a method, then the delegate and the method being pointed to must have the same signature (Signature, with the same parameter list, same return value). Therefore, the System.EventHandler type and the This.button1_click method have the same signature, and below, let's look at what the signature of the System.EventHandler delegate looks like:

public delegate void EventHandler (

Object sender,

EventArgs E

)

System.EventHandler's signature is: The return value is void; there are two parameters, Object sender, EventArgs e. So the Button1_Click method also has the same form, the code is as follows:

private void Button1_Click (object sender, EventArgs e)

{

}

In fact, we can see the signature of the event callback method is basically not much, but the second parameter is slightly different, below, we describe the method's parameters.

Øobject Sender

From the name of this parameter, you can see its role, sender (sender) means: Who triggered the event, then sender is who, because all types in theory can include events, so the type of sender is defined as the object type, When multiple events point to an event callback method, the parameter can be used to distinguish which class is triggering the event, in order to make different processing, at this time, the parameter sender needs to make a type conversion.

L Case Action 020603: Multiple events point to the same callback method

First, add three buttons, a textbox

The interface is as follows:

Then, add a method ButtonClick in the main window, and the click event of the three buttons will call the method.

The code is as follows:

protected void ButtonClick (object sender, EventArgs e)

{

Button BT = sender as Button;

This.textBox1.Text = "I am:" + BT. Text;

}

In the above code, in order to know which button is clicked, we convert the sender to the button type.

The Click event callback method for these three buttons is specified below

First, switch to the Button1 Properties window (F4), click the "" button, locate the "click" Event, and set the called method named ButtonClick as shown in.

Then, set the Button2,button3 click event in the same way, and they all point to the ButtonClick method.

Finally, run the program, and the following are the operating conditions:

Click Button1:

This.button1.Click + = new System.EventHandler (This.button1_click);

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.