Delegate. event. Lambda (2)

Source: Internet
Author: User

In C #, a class can contain fields, properties, methods, indexes, and events, the events member declares a class event. The following syntax is used to declare an event member in a class: public event represents the name of the event.


1. A simple delegate

   Delegate     Int  Delegateoperation ( Int  I1,  Int  I2 );
Static Void Main ( String [] ARGs)
{
Delegateoperation do1 = New Delegateoperation (ADD );
Console. writeline (do1 ( 100 , 200 ));
Console. Readline ();
}
Public Static Int Add ( Int M, Int N)
{
Return M + N;
}

 

according to the definition of the Delegate, we can know the result of the Code section. Delegateoperation --> Add ( int m, int N) --> Add ( 100 , 200 ). That is, the process goes down.

 
2. Event implementation process

   Private     Void  Button33_click (  Object  Sender, eventargs E)
{
Delegateclass DC = New Delegateclass ();
DC. registerok + = New Delegateclass. delegateregisterokevent (s1_registerok );
DC. Register ();
}

Void S1_registerok ()
{
MessageBox. Show ( " Trigger the registerok event " );
}

 

   Class Delegateclass
{
Public Delegate Void Delegateregisterokevent ();
Public Event Delegateregisterokevent registerok;
Public Void Register ()
{
MessageBox. Show ( " Call the registerok event. " );
Registerok ();
}
}

 

The following code is a class with delegation and events. The above code is stored in the main function. DC. Register () --> registerok () --> s1_registerok --> ("trigger registerok event ").

 


3. C # pre-define the event handling method  

Private VoidTextbox1_keypress (ObjectSender, system. Windows. Forms. keypresseventargs e ){
}
This. Textbox1.keypress+ =Newsystem. Windows. Forms. keypresseventhandler (This. Textbox1_keypress );


Some events in C # may appear different from the previous ones. For example, private void textbox1_keypress (Object sender, system. windows. forms. keypresseventargs e) {} This. textbox1.keypress + = Newsystem. windows. forms. keypresseventhandler (this. textbox1_keypress); here keypresseventargs is used instead of eventargs as the parameter. The keyeventhandler delegate is used here, instead of the eventhandler delegate. Keypresseventargs is the derived class of eventargs. The keyeventhandler declaration is as follows: Public Delegate void keyeventhandler (Object sender, keyeventargs E); it is the delegate that the parameter is keyeventargs. So why does the keypress event do this? We can find the answer from the constructor of two classes. Public eventargs (); Public keypresseventargs (char keychar); The keydata here is used to pass the key we pressed. In keyeventargs, I found that the attribute public char keychar {Get;} further proves my theory. Let's take a similar example to help you understand it. This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/fangyuan303687320/archive/2010/01/02/5122103.aspx

Some events in C # may appear different from the previous ones. Keypresseventargs is used here instead of eventargs as the parameter. The keyeventhandler delegate is used here, instead of the eventhandler delegate. Keypresseventargs is the derived class of eventargs, And the keyeventhandler declaration is as follows:

 
   Public Delegate VoidKeyeventhandler (ObjectSender, keyeventargs E );



4. Use of event parameters

The example above is as follows:

     Public     Class  Keypresseventargs: eventargs
{
// Abstract:
// Initialize a new instance of the system. Windows. Forms. keypresseventargs class.
//
// Parameters:
// Keychar:
// ASCII character corresponding to the key pressed by the user.
Public Keypresseventargs ( Char Keychar );

// Abstract:
// Gets or sets a value indicating whether the system. Windows. Forms. Control. keypress event has been processed.
//
// Returned results:
// True if the event is processed; otherwise, false.
Public Bool Handled { Get ; Set ;}
//
// Abstract:
// Gets or sets the character corresponding to the pressed key.
//
// Returned results:
// ASCII character. For example, if you press SHIFT + K, this attribute returns an uppercase K.
Public Char Keychar { Get ; Set ;}
}

 

HereKeypresseventargsExtendedEventargs.AddedHandledAndKeycharTwo attributes. In this way, more information can be obtained when this event is triggered.

 

 

 

 

    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.