To construct an event for a class, you must declare an delegate type field with event, such as:
Puclic CALSS test{
Public delegate EventHandler (object sender, EventArgs e); An event declared to be a delegate type;
}
Then you specify the name of an event and write out the processing statement:
Public event EventHandler Load
Define this "Load" event after creating an instance of the class:
Test m=new test ();
M.load=new EventHandler (m_load);
void M_load (object sender, EventArgs e)
{
MessageBox.Show ("This is a class event");
}
Take a look at the complete code below:
Using System;
Class TestClass
{
static void Main (string[] args)
{
EventClass Myeventclass = new EventClass ();
Myeventclass.customevent + = new Eventclass.customeventhandler (CUSTOMEVENT1); Associated event handle;
Myeventclass.customevent + = new Eventclass.customeventhandler (CUSTOMEVENT2);
Myeventclass.invokeevent ();
Myeventclass.customevent-= new Eventclass.customeventhandler (CUSTOMEVENT2);
Myeventclass.invokeevent ();
Myeventclass.load + = new Eventclass.customeventhandler (LOAD1);
Myeventclass.onload ();
}
private static void CustomEvent1 (object sender, EventArgs e)
{
Console.WriteLine ("Fire Event 1");
}
private static void CustomEvent2 (object sender, EventArgs e)
{
Console.WriteLine ("Fire Event 2");
}
private static void Load1 (object sender, EventArgs e)
{
Console.WriteLine ("Current background The color is {0}.") Please input: ", System.Console.BackgroundColor.ToString ());
}
}
public class EventClass
{
public delegate void Customeventhandler (object sender, EventArgs e);//First defines an object of a delegate type Customeventhandler
Declare an event with the delegate data type, with the event keyword, which defines two pieces of text;
public event Customeventhandler Customevent;
public event Customeventhandler Load;
public void Invokeevent ()
{
Customevent (this, eventargs.empty);
}
public void OnLoad ()
{
Console.backgroundcolor = consolecolor.red;
Load (this, eventargs.empty);
string s = Console.ReadLine ();
if (S!= "yuping")
{
Console.WriteLine ("You must type ' yuping ' for change it!");
}
Else
{
Console.backgroundcolor = System.ConsoleColor.Black;
Console.clear ();
}
}
}
In the class that contains the event declaration, declare a data type is a delegate of such an object Customeventhandler, it has two parameters (sender and E); The purpose of using a delegate here is to pass the method to the parameter in the run. And the instance generated by the delegate object receives the return value of this parameter method, therefore, when declaring an object of a delegate type, it should be defined according to the method structure in the class, or in a reference class the method structure that responds to the event should be generated based on the structure of the delegate object, such as what types of arguments they have, the type of the return value, That means the two should be consistent. Also, to properly use a delegate in C #, you must maintain three steps: declaration-instantiation-invocation.
In the above code, the EventClass class embodies this principle:
1. Object declaring the delegate type: public delegate void Customeventhandler (object sender, EventArgs e);
2. Create an instance of the Customeventhandler object Customevent:public event Customeventhandler customevent;
3. A call to the event is implemented in the Invokeevent () method, referencing the event.