C # custom events

Source: Internet
Author: User

Custom
Event
It

Using system;

 

Delegate viod chareventhandler (Object sender, chareventargs E );

 

Custom parameters for triggering an event

Public class chareventargs eventargs

{

Public char currchar;

Public chareventargs (char currchar)

{

This. currchar = currchar;

}

}

 

Class charchecker

{

Char curr_char;

Public event chareventhandler chartest;

 

Public char curr_char

{

Get {return curr_char ;}

Set

{

If (chartest! = NULL)

{

Chareventargs myeven = new chareventargs (value );

Chartest (this, myeven); triggers the event here

Curr_char = myeven. currchar

}

}

}

}

 

Class appevent

{

Static void main ()

{

Charchecker chartester = new charchecker ();

Chartester. chartest + = new chareventhandler (change_x); when the chartest event is triggered, it is bound to the change_x function.

 

Chartester. curr_char = 'a'; run the Set Method of curr_char to trigger the event.

Console. writeline (event processing result: {0}, chartester. curr_char );

Chartester. curr_char = 'B ';

Console. writeline (event processing result: {0}, chartester. curr_char );

Chartester. curr_char = 'X ';

Console. writeline ({0}, chartester. curr_char );

Console. writeline ();

}

Static void change_x (Object source, chareventargs E)

{

If (E. currchar = 'X' E. currchar = 'X ')

{

Console. Write (the trigger character is X ,);

Console. Write (replace X :);

E. currchar = '';

}

Else

Console. Write (the trigger character is not X ,);

}

}

 

 

Running result:

The trigger character is not X. The event processing result is.

The trigger character is not X, and the event processing result is B.

The trigger character is X. Replace X :?

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.