C # event mechanism induction (II)

Source: Internet
Author: User

Last article: C # event mechanism induction (I)

3. C # pre-define the event handling method

I think the most difficult thing to understand about learning events is that the pre-defined events in C # make me confused when learning events. After checking some information, I finally figured out the following:

EventArgs is the base class of the class containing event data, used to pass event details.

EventHandler is a delegate declaration as follows (which is declared as follows in the. Net Class Library)

Public delegate void EventHandler (object sender, EventArgs e)

Therefore, all shapes are as follows:

Void functions (object parameter name, EventArgs parameter name) can be used as the Click Event Response Method of the Control class. As defined below, an event response method:

Private void button#click (object sender, System. EventArgs e)

The object sender parameter indicates the object that triggers the event. (In fact, the reference of the object is passed here. If it is a click event of button1, sender is button1.) System. eventArgs e indicates the event information, such as the x and y values of the mouse.

Next, let's take a look at the event declaration in the Button class. The Click event is used as an example.

Public event EventHandler Click;

An EventHandler event Click is defined here.

Private void button#click (object sender, System. EventArgs e)
{
...
}

This is the method corresponding to the button#click event. Note that the parameters of the method comply with the signature in the delegate (both the parameter list ). Then how can we associate this method with the event? Please refer to the following code.
This. button1.Click + = new System. EventHandler (this. button#click); (in fact, button1.Click is an instance event assigned by System. EventHandler. This method is very similar to the method used to delegate an instance to a delegate)
Bind this. button#click method to this. button1.Click event.

4. Use of event parameters.

Using System;

Class Class1

{
Static void Main ()
{
Student s1 = new Student ();

S1.Name = "Student1 ";

Student s2 = new Student ();

S2.Name = "Student2 ";

S1.RegisterOK + = new Student. DelegateRegisterOkEvent (Student_RegisterOK );

S2.RegisterOK + = new Student. DelegateRegisterOkEvent (Student_RegisterOK );

// When Register Method 1 is executed, the RegisterOK event is triggered.

// The RegisterOK event is triggered and the Student_RegisterOK method is executed.

S1.Register ();

S2.Register ();

Console. ReadLine ();
}
Static void Student_RegisterOK (RegisterOkArgs e)
{
Console. WriteLine (e. EventInfo );
}
}
Class Student

{

Public delegate void DelegateRegisterOkEvent (RegisterOkArgs e );

Public event DelegateRegisterOkEvent RegisterOK;

Public string Name;

Public void Register ()

{
Console. WriteLine ("Register Method ");
RegisterOK (new RegisterOkArgs ("Student Name:" + Name ));
}
}

Class RegisterOkArgs: EventArgs

{
Public string EventInfo;

Public RegisterOkArgs (string eventInfo): base ()
{
This. EventInfo = eventInfo;
}
}

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.