C # custom events

Source: Internet
Author: User

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 );}}

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.