[C # Basic Knowledge series] Topic 5: events behind the click event are triggered when a button is clicked.

Source: Internet
Author: User

Introduction:

When we click the button control vs in the window, it will automatically generate some code, we only need to write some code in the click method to trigger the click event. Then the code in the click method will be executed, however, I have always had a question: since the previous topic mentioned that an event is a multicast delegate, the automatically generated code only involves event instantiation, but does not see event calls, so since there is no code called by the event, why is the encapsulated click executed?

1. Events sent behind the click event are triggered when a button is clicked

I raised my question in the introduction. I believe some of my friends may have such questions. Then, the event must have been called, but it is not called in our code, instead, events are called in the internal code of the butoon control, which leads to the call of the click method encapsulated by the delegate. This meets the situation we see. After clicking the button, the click method in the background code is executed. To understand what happened behind the scenes, let's explore it together?

Create a Windows form program, drag a button control in the form, and click the button. At this time, vs generates the following code for us:

Private system. windows. forms. button button1; private void initializecomponent () {his. button1 = new system. windows. forms. button (); this. button1.location = new system. drawing. point (105, 89); this. button1.name = "button1"; this. button1.size = new system. drawing. size (75, 23); this. button1.tabindex = 0; this. button1.text = "Click me"; this. button1.usevisualstylebackcolor = true; this. button1.click + = new system. eventhandler (this. button#click);} // background code private void button#click (Object sender, eventargs e ){}

From the code above, we can see that vs automatically creates a button object for us and instantiates it. It sets its attributes and passesThis. button1.click + = new system. eventhandler (this. button#click); in this line of codeRegistration pays attention to the click event. However, where is the event calling code? Next we will set the breakpoint in the button#click method to see how the code is executed (by viewing the call stack to see the code execution sequence). Below is a call stack where I set the breakpoint:

From this, I found that execution is required before the button#click method is called.Control. onclick (system. eventargs E)Method, then we can use the launch tool to viewControl. onclick (system. eventrgs E)The internal code of The onclick method is:

We can understand from the reflected code that, first of allEvents(You can use the reflection tool to viewEventsType. Its type isEventhandlerlist, AndEventhandlerlistIt is also a sealed class) to retrieve the delegate from the delegate set. If the Click Event (delegate) is instantiated, it will not be empty. At this time, the Delegate will be called --Handler (this, e), we know that we usedThis. button1.click + = new system. eventhandler (this. button#click); the Code instantiates a delegate event, so the button#click method encapsulated by eventhandler is executed.

Through the above explanation, I have removed my initial doubts. The event is called in. net class library. called in The onclick method, which is what is done behind the click event that I want to express
The code for the click event is as follows:

 

Ii. Summary

In this topic, I first raised my doubts about what happened behind the button-clicking event. I tried debugging and reflection tools to touch the issue step by step. I believe other events of other controls are the same, this topic is intended for you to understand. net class library is what we do. We hope that some beginners will try to understand the essence of things when learning about things. Finally, I hope this topic will allow you to further understand the nature of the event. I will share with you the next topic what I understand about generics.

Reflection tool: http://files.cnblogs.com/zhili/Reflector.zip

 

 

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.