Analysis of Visual C # Event Processing Mechanism (Reprinted)

xiaoxiao2021-03-06  113

Event Description: Any programmers developed by graphical user interface will know the concept of events. When the user is using the program, the user must interact with the program. For example, when a user clicks on a button on the form, the program generates the button clicked event and responds to the user's operation through the corresponding event handler. This way, the user's intuitive feeling is that the program has implemented the task I have requested. Of course, the event does not necessarily be generated in the case of user interaction, and the inside of the system also generates some events and requests processing, such as clock events is a good example. However, it is necessary to introduce the event handling mechanism in C # (extended to a wider range is the entire .NET framework), we must first understand a concept called "entrusted".

Entrust in C #:

Entrustment, as the name suggests, the meaning of the middle agents. The commission in the C # allows you to pass on the method in an object to another object that can call the class of the method. You can pass a method M (included in a delegate) in a class A, so that class B can call the method M in class A. At the same time, you can also pass the method in a static manner or in an instance (instance). So this concept is very similar to the concept of a function pointer in the C to call other classes in other classes.

The concept of the entrusted first is presented in Visual J , now C # also applies the concept of entrusted, which is also to "brought". " The delegate in C # is implemented by inheriting a class in System.Delegate, below is the specific steps:

1. Declare a delegate object, its parameters must be consistent with the parameters of the methods you want to include.

2. Define all the methods you want to define, the parameters of its parameters must be the same.

3. Creating a delegate object and inclusion the desired method is included in the delegate object.

4. The various methods included in it are invoked by the delegate object.

The following C # code shows how to use four steps to implement the entrust mechanism:

Using system; file: // Step 1: Declare a delegate object public delegate void mydelegate (String Input); File: // Step 2:: Define each method, its parameter form and the entrusted object declared in step 1 must be the same 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);}} file: // Step 3: Create a delegate object and will contain the above method contains Class myclass2 {public mycate creatededeelegate () {myclass1 c2 = new myclass1 ); MyDelegate D1 = new mydlegate (c2.delegateMethod1); MyDelegate D2 = new mydlegate (c2.delegateMethod2); MyDelegate D3 = D1 D2; RETURN D3;}} file: // Step 4: Calling through the delegate object included in it the method of 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");}} Event processing function in C #:

The event handler in the C # is a delegate object with a specific parameter form, which is as follows:

Public Delegate Void MyEventHandler (Object Sender, MyEventArgs E);

The first parameter indicates the object that triggers the event, and the second parameter (e) contains some of the data that can be applied in the event handler. The above Myeventargs class is inherited from the Eventargs class, and the latter is some broader class, such as the base class of the MouseEventArgs class, the ListChangeDeventargs class. For GUI-based events, you can use these more widely, the object that has been defined, and you must complete your own class for those who are non-GUI-based events, and will be derived from the Eventargs class. The data to be included is passed to the delegate object. Here is a simple example:

Public class myeventargs Eventargs {public string m_myeventargumentdata;

In the event handler, you can reference the delegate object by keyword Event, the method is as follows:

Public evenet myeventhandler myevent;

Now let's create two classes, we can know how C # complete event processing is working. In our instance, the Class A will provide an event's processing function, and create a delegate object in step 3, which is included in it, and the parameters of the event processing function must be in the parameter form of the delegate object, in step 3 Consistent. The Class A will then pass the delegate object to Class B. When the events in the B class are triggered, the event handler in the A class is called correspondingly. Here is sample code: use system; file: // Step 1: Declaration Privilege Object PUBLIC DELIC DELIC DELID MyHandler1 (Object Sender, MyEventAndler2); File: // Step 2: Created Method of event processing function 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);} File: // Step 3: Create a delegate object, and The function contains the object to trigger the event. Public A (b) {MyHandler1 D1 = New MyHandler1 (OnHandler1); MyHandler2 D2 = New MyHandler2 (OnHandler2); B.Event1 = D1; B.Event2 = D2 ;}} File: // Step 4: Call the included method class b {public1; public void fireevent1 (myEventArgs e) {if (Event1! "for public event myHandler2 Event2; Public Void FireEvent1; Public Void FireEvent1; Public Void FireEvent1; = NULL) {Event1 (this, e);}} public void fireevent2 (myEventargs e) {if (Event2! = null) {Event2 (this, e);}}}} public class myeventargs Eventargs}}}}}}} PUBLIC STRING M_ID;} 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 event handler of the GUI in C #: Completes the basic method of the event handler under the GUI and there is not much difference in the above introduction, and we will complete a simple instance program by the above method. The primary class MyForm class of this instance program is inherited from the Form class. By observing the entire code and related annotations, you can find that we have not given it a delegate object and reference the delegate object through the Event keyword, that is because the GUI control has long helped us to do this work, its commission The object is System.EventHandler. However, we still have to define methods for each control (that is, the event's handle function) and contains them in the System.EventHandler. In that, when the user and program interacts, the corresponding event handler will be triggered. The specific code is as follows:

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"; File: // Create a delegate object, containing the method and assigns the Click event m_namebutton.click = new system.eventhandler (NameButton.Location = New Point); M_ClearButton.Location = new point 16, 152); m_clearbutton.size = new size (176, 23); m_clearbutton.text = "clear"; file: // Create a delegate object, containing the method and assigns the Click event m_clearbutton.click of the button to the button, = new system . EventHandler (ClearButtonClicked); this.clientsize = new s Ize (292, 271); this.controls.addrange (new control [] {m_namelabel, m_namebutton, m_clearbutton}; this.ResumeLayout (false);} file: // definition method (Event processing function), its parameter form must be the same private void NameButtonClicked delegate object (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 () {Application.run (new myform ());}}

转载请注明原文地址:https://www.9cbs.com/read-102678.html

New Post(0)