An analysis of event handling and custom events in C #
Source: Internet
Author: User
First, understand the predefined event handling mechanism in C #
Before writing code, let's familiarize ourselves with the classes and delegates in the. NET Framework that are related to events, and understand the handling of predefined events in C #.
EventArgs is the base class for the class that contains the event data to pass the details of the event.
EventHandler is a delegate statement as follows
public delegate void EventHandler (object sender, EventArgs e)
Note the argument here, the former is an object (in fact, this is passing the object reference, if the Button1 click event is sender is Button1), followed by the class containing the event data base class.
Let's look at the button class to see the event declarations (see Using the Wincv tool), and take the Click event as an example.
public event EventHandler Click;
This defines a EventHandler type of event click
The previous content is C # that has been defined for us in the class library. Let's look at the code generated in programming.
This is the way we correspond to the Button1_Click event. Note that the parameter of the method conforms to the signature in the delegate (both the argument list). So how do we relate this method to the event, please look at the code below.
This.button1.Click + = new System.EventHandler (This.button1_click);
Bind the This.button1_click method to the This.button1.Click event.
Let's look at the workflow of C # event processing, and first, the system will create an object for us to listen for events in the background (if the event is button1, the listener is button1), this object is used to generate the event, and if a user event occurs, the corresponding application event is generated. Then execute all the methods that subscribe to the event.
Ii. Simple custom events (1)
First we need to define a class to listen for client events, where we listen for input from the keyboard.
Defines a delegate.
public delegate void Userrequest (Object Sender,eventargs e);
The previous object is used to pass the event's occurrence, and the later EventArgs is used to pass the details of the event, which is now useless, and will be used later in the example.
The following defines an event for this delegate type type
public event Userrequest Onuserrequest;
Now let's do a dead loop.
public void Run () {bool Finished=false. {if (console.readline () = = "H") {Onuserrequest (this,new EventArgs ());} while (!finished); }
This code constantly requires the user to enter a character, and if the result of the input is H, the Onuserrequest event is triggered, the event's trigger is itself (this), and the event details are not (no EventArgs instance passing any arguments). We give this class the name Userinputmonitor.
Here's what we're going to do is define the client's class
First we have to instantiate the Userinputmonitor class
Userinputmonitor monitor=new userinputmonitor ();
The last thing to do is to associate this method with the event (subscribe to the event), and we'll write it to the constructor of the library-side class.
Note that this type of writing is wrong, because the delegate is static
}
The following creates an instance of the client.
New Client (monitor);
By the right, don't forget to let monitor start listening to the event.
Monitor.run ();
Done, the code is as follows:
Using System;class userinputmonitor{public delegate void Userrequest (Object Sender,eventargs e);//define Delegate public event User Request onuserrequest; Event public void Run () {bool Finished=false. Do {if (console.readline () = = "H") {Onuserrequest for this delegate type type (This,new EventArgs ()); }}while (!finished); }}
public class client{public static void Main () {userinputmonitor monitor=new userinputmonitor (); new Client (monitor); Mon Itor. Run (); } private void ShowMessage (Object Sender,eventargs e) {Console.WriteLine ("haha!!");} Client (userinputmonitor m) {m.onuserrequest+=new userinputmonitor.userrequest (this. ShowMessage); M.onuserrequest+=new M.userrequest (this. ShowMessage); Note that this type of writing is wrong because the delegate is static}
Third, further study the predefined event handling mechanism in C #
You may find that some of the events in C # seem to be different from those in front. For example
This uses the KeyPressEventArgs instead of the EventArgs as a parameter. The Keyeventhandler delegate is used here instead of the EventHandler delegate.
KeyPressEventArgs is a derived class of EventArgs, and the Keyeventhandler declaration is as follows
public delegate void Keyeventhandler (object sender, KeyEventArgs e);
is a delegate with a parameter of KeyEventArgs. So why do keypress events do this, we can look for answers from the constructors of two classes.
public EventArgs ();
Public KeyPressEventArgs (char Keychar);
Here's what the Keydata is, it's used to pass the key we pressed, ha.
I found the attribute in the KeyEventArgs.
Public Char Keychar {get;}
Further proof of my theory. Let's do a similar example to help understand.
IV. Simple custom events (2)
Take the example we made above to change it.
We also define a EventArgs (similar KeyEventArgs) name MyEventArgs, define a constructor public MyEventArgs (Char Keychar), and we also set the corresponding properties. The code is as follows
Using System;class mymyeventargs:eventargs{Private char Keychar; public Mymyeventargs (char Keychar) {this.keychar= Keychar; Public Char Keychar {get {return keychar;}}}
Because it's time to listen for multiple keys, we have to overwrite the do...while part of the listener's class. Overwrites the delegate, overwriting the parameters passed by the client. Okay, the final code is as follows, so tired
Using System;class myeventargs:eventargs{Private char Keychar; public MyEventArgs (char Keychar) {This.keychar=keychar; Public Char Keychar {get {return keychar;}}}
Class userinputmonitor{public delegate void Userrequest (Object Sender,myeventargs e);//define Delegate public event Userrequest OnU Serrequest; Event public void Run () {bool Finished=false. Do {string inputstring= console.readline () for this delegate type type, or if (inputstring!= "") on Userrequest (this,new MyEventArgs (inputstring[0)); }while (!finished); }}
public class client{public static void Main () {userinputmonitor monitor=new userinputmonitor (); new Client (monitor); Mon Itor. Run (); private void ShowMessage (Object Sender,myeventargs e) {Console.WriteLine ("Snap to: {0}", E.keychar);} Client (userinputmonitor m) {m.onuserrequest+=new userinputmonitor.userrequest (this. ShowMessage); M.onuserrequest+=new M.userrequest (this. ShowMessage); Note that this type of writing is wrong because the delegate is static}
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.