Understand the events in C #: Simulate the trigger process of Button events in C #

Source: Internet
Author: User

During Development on weekdays, when coding the logic after clicking a button, we have to help vs with its powerful functions. If you double-click the button, a time method is automatically generated in the code, then we only need to generate the method as follows:

 

Privatevoid button#click (Object sender, eventargs E)
{
// To do
}

You only need to write your own logic in to do. I believe most people will also use button1.click + = new eventhandle (button#click) to bind the event Method to the button.

Now I want to write a demo to simulate this process, and hope to have a better understanding of Button event triggering. By the way, I am also familiar with the. NET event model.

Code

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace eventdemo
{
/// <Summary>
/// Event Delegate
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Publicdelegatevoid eventhandle (Object sender, eventargs E );

Class Program
{
Staticvoid main (string [] ARGs)
{

Button BTN = new button ();
BTN. Click + = new eventhandle (btn_click );
Trigger (BTN, eventargs. Empty); // equivalent to trigger by clicking the mouse
Console. Read ();
}

Staticvoid btn_click (Object sender, eventargs E)
{
Button BTN = (button) sender;
BTN. Text = "I was clicked ";
Console. writeline (BTN. Text );
}

/// <Summary>
/// After you click the button in the form, Windows will send a wm_mouseclick message to the button Message Processing Program (wndpro ).
/// </Summary>
/// <Param name = "BTN"> </param>
/// <Param name = "E"> </param>
Staticvoid trigger (Button BTN, eventargs E)
{
BTN. onclick (E );
}
}

/// <Summary>
/// Simulate the button
/// </Summary>
Publicclass button
{
Privatestring txt;
Publicstring text
{
Get {return txt ;}
Set {TXT = value ;}
}

Publicevent eventhandle click; // defines the time when the button is clicked.

/// <Summary>
/// Event execution Method
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Void actionevent (Object sender, eventargs E)
{
If (Click = NULL)
Click + = new eventhandle (err_actionevent );
Click (sender, e); // This part executes the btn_click method in the program.
}

Void err_actionevent (Object sender, eventargs E)
{
Thrownew exception ("the method or operation is not implemented .");
}

Publicvoid onclick (eventargs E)
{
Actionevent (this, e );
}

}

}

The above is the whole process of simulating the mouse clicking and then executing the event function.

After you click the button in the form, Windows will send a wm_mouseclick message to the button Message Processing Program (wndpro). For. NET developers, It is the onclick event of the button.
Discuss events from the customer's perspective:
An event receiver is any application, object, or component that is notified when something occurs (in the demo above, it can be understood as an instance of the button class BTN ). If there is an event receiver, there is an event transmitter. The sender is used to initiate an event. A sender can be another object or assembly in an application (in the demo above, it can be understood as a program in the Assembly that triggers an event). In actual system events, such as mouse clicking or keyboard pressing, the sender is.. Net Runtime Library. Note that the event Sender does not know who the receiver is. This makes the event very useful.
Now there is a method somewhere in the event receiver (onclick in the demo) that processes the event. Execute the event handler (btn_click) every time a registered event occurs ). In this case, the delegate is required. Since the sender does not know anything about the receiver, the reference type between the two cannot be set. Instead, the sender uses the delegate as the intermediary, which defines the delegate to be used by the receiver and registers the event handler to the event, the process of accepting Event Handlers becomes an encapsulated event.

[Reference: red book version 4 C # advanced programming]

Related Article

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.