First, understand the predefined event handling mechanism in C #
Before writing the code, let's first familiarize yourself with the .NET framework and the event-related class and delegate to understand the processing of predefined events in C #.
Eventargs is a base class that contains the class of event data for passing the details of the event.
EventHandler is a delegate statement as follows
Public Delegate Void EventHandler (Object Sender, Eventargs E)
Note that the parameters here, the former is an object (in fact, it is a reference to the object. If the CLICK event of Button1 is Button1), and the subsequent class is the base class containing the class.
Below we have studied the BUTTON class to see the event declarations (viewed using WinCV tools), take Click event as an example.
Public Event even Click;
Here, an eventHandler type event Click
The previous content is C # in the class library has been defined for us. Let's look at the code generated when programming.
Private void button1_click (object sender, system.eventargs e) {...}
This is a method corresponding to the Button1_Click event. Note The parameters of the method are in line with the signature in the delegate (for both the parameter list). Then how do we connect this method and an event, please see the following code.
This.Button1.click = new system.eventhandler (this.button1_click);
Bind this.button1_click method to this.button1.click event.
Below we have studied the workflow for C # event processing. First, the system will create an object to listen to the event in the backpack (if it is the event of Button1), this object is used to generate an event, if there is a certain User events generate the corresponding application event, and then perform all methods of subscribing to the event.
Second, simple custom events (1)
First we need to define a class to listen to the client event, here we listen to the input of the keyboard.
Define a delegate.
Public Delegate Void UserRequest (Object Sender, Eventargs E);
The front Object is used to transmit the incident, the end of Eventargs is used to pass the details of the event, and now there is no use, and will be used later.
The following is the event of this delegate type type
Public Event UserRequest OnuserRequest;
Let's take a dead cycle.
Public void Run () {bool finished = false; do {ix (console.readline () == "h") {onuserRequest (this, new evenetargs ());}} while (! "
This code is constantly requested by the user to enter characters. If the result is h, the ONUSERREQUEST event is triggered, the trigger of the event is itself (this), the event details are not (did not pass any parameter Eventargs instance). We have named this catere for UserInputmonitor.
Below we have to do is to define the classes of the client to instantiate the USERINPUTMONITOR = New UserInputMonitor ();
Then we define a method.
Private Void ShowMessage (Object Sender, Eventargs E) {Console.writeline ("Haha !!");} The last thing to do is to link this method and event (subscribe event), we write it to the library In the constructor.
Client (userinputmonitor m) {m.onuserRequest = new userinputmonitor.userRequest (this.showMessage); //m.onuserRequest =new m.userRequest (this.ShowMessage);
// Note that this way is wrong, because the delegation is static
}
Create an instance of the client below.
New Client (Monitor);
Yes, don't forget to let Monitor begin to listen.
Monitor.Run ();
Dache, the code is as follows:
using System; class UserInputMonitor {public delegate void UserRequest (object sender, EventArgs e); // define a delegate public UserRequest OnUserRequest event; // this type of event delegate type public void Run () {bool finished = false; do {if ( Console.readLine () == "h") {onuserRequest (this, new evenetargs ());}} while (! Finished);}} public class client {public static void main () {userinputmonitor monitor = new userInputMonitor () New client (Monitor); Monitor.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 way is wrong, because the delegate is static}} III, further study a predefined event in C # Process mechanism
You may find that some events in C # do not seem to be just like. E.g
Private void textbox1_keypress (Object sender, system.windows.Forms.keypressEventArgs e) {
}
This.TextBox1.KeyPress = newsystem.windows.Forms.KeyPressEventHandler (THISTEXTBOX1_KEYPRESS);
The keypressEventargs is used here instead of Eventargs as parameters. Here, KEYEVENTHANDLER is used here, not the EventHandler commission.
KeypressEventArgs is the derived class of Eventargs, and KeyEventHandler's statement is as follows
Public Delegate Void KeyeventHandler (Object Sender, KeyEventargs E); is a delegation for KeyEventArgs. What is the keypress event to do this, we can find the answer from two classes of constructor.
Public evenetargs ();
Public keypressEventArgs (char keychar);
What is the keydata here, which is used to pass on which key is pressed, ha.
I found the property in KeyEventArgs.
Public char keychar {get;}
Further proved my theory. Let's take a similar example to help understand.
Fourth, simple custom events (2)
Take the example of our above to change.
We also define an EventArgs (similar KeyEventArgs) name MyEventArgs, define a constructor public myeventargs (char keycha), and we also set the corresponding properties. code show as below
Using system; class mymyeventargs: Eventargs {private char keycha; public mymyeventargs (car keychar) {this.keychar = keychar;}}}}}}}}}
Because now we have to listen to multiple keys, we have to change the DO ... While section in the class of the listener. Rewriting the delegate to change the parameters passed by the client. Well, 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 the principalRequest; // This entrustment type type of event public void Run () {bool finished = false; do {string inputstring = console.readline (); if (inputstring! = "" OnUserRequest (this, new MyEventArgs (inputString [0]));} while (finished!);}} public class Client {public static void Main () {UserInputMonitor monitor = new UserInputMonitor (); new Client (monitor); monitor. Run ();} private void ShowMessage (object sender, MyEventArgs e) {Console.WriteLine ( "captured: {0}", e.KeyChar);} Client (UserInputMonitor m) {m.OnUserRequest = new UserInputMonitor.UserRequest ( This.ShowMessage); //m.onuserRequest ; // Note that this way is wrong, because the delegate is static}} The level is limited, so it is inevitable, it is extremely welcome to criticize everyone Finger.
Renrenqq (DDLLY) Email: ddlly@tom.com, QQ24008122