C # event mechanism summarizes (below) changqi [original]

zhaozj2021-02-16  74

3. Predefined event processing method in C #

I feel that the least understanding is that the event is predefined in C #, so I started to learn the incident. After checking some information, I finally understood some, as follows:

Eventargs is a base class that contains the class of event data for passing the details of the event.

EventHandler is a delegate statement (which is declared in the .NET class library)

Public Delegate Void EventHandler (Object Sender, Eventargs E)

So, all words are:

Void letter (Object parameter name, Eventargs parameter name);

The function can be used as a CLICK event response method for the Control class. An event response method as defined below:

Private void Button1_Click (Object Sender, System.Eventargs E)

The parameter Object Sender indicates an object that raises the event, (in fact, it is the reference to the object, if it is the Click event of Button1, sender is button1) System.Eventargs E represents the corresponding information of the event, such as the X, Y value, etc. of the mouse.

Below we have studied the BUTTON class to see the event declarations, take the Click event as an example.

Public Event even Click;

Here, an eventHandler type event ClickPrivate void button1_click (Object Sender, System.EventArgs E) {...} This is the method corresponding to the Button1_Click event. Note The parameters of the method are in line with the signature in the delegate (for both the parameter list). Then how do we connect this method and an event, please see the following code. This.Button1.Click = new system.eventhandler (this.button1_click); (in fact, Button1.Click is the instance event for the system.eventhandler delegated. It is very similar to the delegation to delegate the instance to a method) THIS.BUTTON1_CLICK method is tied. Set to this.button1.click event.

4. The use of the parameters of the event.

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 the register method is executed, it triggers the Registerok event.

// Registerok event triggered, then execute a Student_Registerok method

S1.Register ();

S2.Register ();

Console.readline ();

}

Static Void Student_Registerok (Registerokargs E)

{

Console.writeLine (EVENFO);

}

}

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;

}

}

转载请注明原文地址:https://www.9cbs.com/read-15062.html

New Post(0)