C # basic concepts-Events

Source: Internet
Author: User

Events are an important concept in the C # programming model. Through understanding events, you can better understand the program running mechanism of C. Events in C # can be simply understood as one thing happened to a class or object, and the event is notified to other classes or objects, other classes or objects can be reflected Based on the event message. This is very similar to the observer pattern in the design pattern.

The class or object that triggers the event can be called the event source. The class or object that registers and processes the event can be called the event subscriber or event listener. I prefer to call it the event listener.

For example, to build a winform application, simply place a button control in form Form1, and double-click the button to go to the Code view and click the event handler.

   Private VoidButton#click (ObjectSender, EventArgs e)
{
///Sender indicates the event source, and e indicates the message passed through the event.
}

You can also open Form1.designer. cs and find the following code:

this.button1.Click += new System.EventHandler(this.button1_Click);

This line of code indicates that the current form registers a click event (click is an event of the Button object) and is processed using the button#click method. EventHandler is a delegate. For more information about delegation, see my previous article html "> C # delegation of basic concepts

The above is a simple explanation of the C # event. Of course, we can also customize events.

Custom events are divided into the following steps:

Step 1: first define the event in the event source object

 //Define events
Public EventEventHandler myevnet;
Step 2: subscribe to the event listener
  //Event registration
This. Myevnet+ =NewEventHandler (MyEventHandler );

Step 3: Compile the event processing code for the event listener:

  /// <Summary>
///Event Processing
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private VoidMyEventHandler (ObjectSender, EventArgs e)
{
MessageBox. Show ("Myevent is raised .......");
}

Step 4: trigger an event on the event source object

 //Determine whether an event is registered
If(Myevnet! = Null)
{
//Events
Myevnet (This. Button1,NewEventArgs ());
}

This is only a demonstration for your reference. The complete code is provided below to facilitate testing.

Simple custom events and complete code
1     Public Partial ClassForm1: Form
2 {
3 //Define events
4   Public EventEventHandler myevnet;
5
6 PublicForm1 ()
7 {
8 InitializeComponent ();
9 //Event registration
10   This. Myevnet+ =NewEventHandler (MyEventHandler );
11
12 }
13
14
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.