Steps for customizing events:
1. Declaration delegate:
Public Delegate youractioneventhandler (Object sender, arguevent E );
2. Declare events
Public event youractioneventhandler youraction;
3. Registration event:
Class class = new class ();
Class. youraction + = new youractioneventhandler (Object sender, arguevent E );
4. Implement the event processing function:
Public void youractioneventhandler (Object sender, arguevent E)
{
// Here is your code
}
5. trigger events
Public void button#click (Object sender, arguevent E)
{
Youraction (this, arguevent. Empty );
}
InstanceCodeDeclare and trigger events in class2, register in class1, and process events
Class1 {static void main (string [] ARGs) {class2 Ts = new class2 (); Ts. closeaction + = new class2.closeactioneventhandler (ts_closeaction); class2 ts2 = new class2 (); // The second class2 instance is introduced here for comparison with TS. run ();
// Ts2.run (); if this method is executed, the compiler will report that closeaction in class2 has not set the object reference to the object instance because ts2 has not registered the event. Console. read ();} static void ts_closeaction (Object sender, eventargs e) {console. writeline ("test event triggered") ;}} class2 {Public Delegate void closeactioneventhandler (Object sender, eventargs E); public event closeactioneventhandler closeaction; Public void run () {closeaction (this, eventargs. empty );}}