Introduction Any developer who writes a graphical user interface (GUI) software is familiar with event processing programming, when the user interacts with the GUI control (for example, click on the table on the table), one or more is performed as the above event. method. No user participation, the event may also be executed. The event handler is an object method, which is performed according to an event that occurs in the application. In order to understand the event processing mode under the .NET framework, we need to understand the concept of agents. The agent in the agent C # in the C # allows us to pass a class in a class to other class objects. We can package the method M in class A into a proxy, pass to class B, class B can call the method M, static, and instance methods in class A, static, and instance methods can be transmitted. C software developers should be very familiar with this concept. In C , developers can use the function pointer to the sympathy or other class in the form of parameters. The concept of agents is introduced in Visulal J , and then brought to C #. The C # agent in the .NET frame is implemented in the form of a class inherited from system.delegate. Using proxy needs 4 steps: 1, define an input parameter identical agent object as the method to be packaged. 2. Define all input parameters as the same method as the proxy object defined in step 1. 3. Create a proxy object and connect with the way you want to encapsulate. 4. Call the package by proxy object. The following C # code illustrates the four steps above by implementing one agent: use system; // Step 1: Define a proxy object with the encapsulation method Enter parameters PUBLIC DELEGATE VOIDEDELEGATE (STRING INPUT); / / Step 2: Define the method of defined proxy objects Class myclass1 {public void delegateMethod1 (String Input) {Console.WriteLine ("this is delegateMethod1 and the input to the method is {0}", input); } public void delegateMethod2 (string input) {Console.WriteLine ( "This is delegateMethod2 and the input to the method is {0}", input);}} // step 3: creating a proxy object insertion method class MyClass2 {public MyDelegate createDelegate () {MyClass1 c2 = new MyClass1 (); MyDelegate d1 = new MyDelegate (c2.delegateMethod1); MyDelegate d2 = new MyDelegate (c2.delegateMethod2); MyDelegate d3 = d1 d2; return d3;}} // step 4 : Call the packaged method by proxy.
class MyClass3 {public void callDelegate (MyDelegate d, string input) {d (input);}} class Driver {static void Main (string [] args) {MyClass2 c2 = new MyClass2 (); MyDelegate d = c2.createDelegate () Myclass3 c3 = new myclass3 (); c3.calldelegate (D, "Calling the delegate");} The event handler in the Event handler C # in C # is a proxy with a specific input parameter, as shown below: Public Delegate Void MyEventHandler (Object Sender, MyEventArgs E); the first parameter in the above definition specifies the object of the event, the second parameter (e) stores the data to be used in the event handler. The MyEventArgs class is the basic class of the Eventargs class is a more specialized class such as MouseEventArgs, ListChangeDeventargs. For GUI events, we can use these specific EventArgs classes without having to create specific Eventargs classes themselves. However, for non-GUI events, we still need to create our own specific Eventargs class, store data that wants to pass on the agent object. We can create your own specific Eventargs class by inheritance Eventargs class: public class myeventargs eventargs {public string m_myeventargumentdata;} In the event handler, the call to the agent object needs to be used as Event Getting, as shown below: Public Event myeventhandler myEvent; Below we will establish two classes, experience the working principle of the event handling mechanism in the .NET framework. In the second step of discussions on the agent, we are required to define a method of exactly the same input parameter with the defined agent. In our example, class A will provide an event handler (a method of having the same input parameters as the proxy object.), Which will create a proxy object (the third step in the agent discussion) and install the event handler. Class A will then pass the agent object to class B. When there is an event in class B appears, it will execute the event handler method in class A.
Using system; // Step 1: Create a proxy object public delegate void myHandler1 (Object sender, myeventargs e); public delegate void myHandler2 (Object sender, myeventargs e); // Step 2: Create an event handler method Class a {public const string m_id = "Class A"; public void OnHandler1 (object sender, MyEventArgs e) {Console.WriteLine ( "I am in OnHandler1 and MyEventArgs is {0}", e.m_id);} public void OnHandler2 (object sender, MyEventArgs e) {Console.WriteLine ("I am in OnHandler2 and MyEventargs IS {0}", E.M_ID);} // Step 3: Create a proxy, install the event handler, and register with the object of the startup event. Public a (b) {myHandler1 D1 = new myHandler1 (OnHandler1); MyHandler2 D2 = new myHandler2 (OnHandler2); B.Event1 = D1; B.Event2 = D2;}} // Step 4: Call package by agent Methods. class B {public event MyHandler1 Event1; public event MyHandler2 Event2; public void FireEvent1 (MyEventArgs e) {if (Event1 = null!) {Event1 (this, e);}} public void FireEvent2 (MyEventArgs e) {if (Event2! = null) {Event2 (this, e);}}} public class myeventargs Eventargs {public string m_id;} public class driver {public static void main () {b = new b (); a a = new a (b ); Myeventargs e1 = new myeventargs (); myeventargs e2 = new myeventargs (); E1.M_ID = "Event args for Event 1"; E2.M_ID = "Event args for Event 2"; B.fireEvent1 (E1); B .FireEvent2 (E2);}}} The GUI event in the C # uses the event processing in the Windows Forms (supporting the GUI application .NET framework) to use the .NET event processing mode. Let's write a simple application in this mode, which has a MyForm class inherited from the System.Windows.Forms.Form class. If you carefully study the code and the three lines of comments, you will find that we don't have to define a proxy and call these agents using Event Guanjian characters, because we can use the GUI control (table, buttons, etc.) events (mouse click, etc. ), The agent is System.EventHandler.
Of course, we still need to define methods, create a system.eventhandler, and install methods in the proxy object. Once the event occurs, the method will begin execution.
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class MyForm Form {private Button m_nameButton; private Button m_clearButton; private Label m_nameLabel; private Container m_components = null; public MyForm () {initializeComponents ();} private void initializeComponents () {m_nameLabel = new Label (); m_nameButton = new Button (); m_clearButton = new Button (); SuspendLayout (); m_nameLabel.Location = new Point (16,16); m_namelabel.text = "Click Name Button, please"; m_namelabel.size = new size (300, 23); m_namebutton.location = new point (16,120); m_namebutton.size = new size (176, 23 ); m_namebutton.text = "name"; // Create a proxy, and install the method, bundle the agent on the click of the button on the click event m_namebutton.click = new system.eventhandler (namebuttonclicked); m_clearbutton.location = new point (16,152) ; m_clearbutton.size = new size (176, 23); m_clearbutton.text = "clear"; // Create a proxy, install the method, bundle the agent bundle on the click of the button m_clearbutton.click = new System.EventHandler (ClearButtonclicked ); thi S.ClientSize = new size (292, 271); this.controls.addrange (new control [] {m_namelabel, m_namebutton, m_clearbutton}); this.ResumeLayout (false);} // Define the input parameter and agent identical method private void NameButtonClicked (object sender, EventArgs e) {m_nameLabel.Text = "My name is john, please click CLEAR button to clear it";} private void ClearButtonClicked (object sender, EventArgs e) {m_nameLabel.Text = "Click nAME button , please "; public static void main ()}}}}} Ending the concept of other object-oriented programming languages such as Java and SmallTalk, this concept is in C # New introduction, it is derived from C and J .