An analysis of Visual C # event handling mechanism (1)

Source: Internet
Author: User
Tags define include
Introduction to Visual Events:

Any programmer who has developed a graphical user interface will know the concept of the event. When the user in the use of the program, the user must and the program for certain interaction. For example, when a user clicks on a button on a form, the program produces the event that the button is clicked, and responds to the user's action with the appropriate event handler function. So the intuitive feeling of the user is that the program to carry out the task I asked for. Of course, the event is not necessarily in the context of interacting with the user, the system will produce some events and request processing, such as clock event is a good example. However, to introduce the event-handling mechanism in C # (extending to a wider scope is the whole.) NET Framework), we first need to understand a concept called "delegate".

Delegates in C #:

Commissioned, as the name suggests, is the middle agent of the meaning. A delegate in C # allows you to pass a method from one object to another object that can call the method's class. You can pass a method m (contained in a delegate) in Class A to a class B so that Class B can invoke method M in Class A. At the same time, you can pass the method either statically or in an instance (instance) way. So this concept is very similar to the concept of using a function pointer in C + + to invoke a method in another class in the form of a parameter.

The concept of a delegate was first mentioned in Visual J + +, and now C # also applies the concept of a delegate, which is "copycat". The delegates in C # are implemented by inheriting a class in System.Delegate, and the following are the specific steps:

1. Declare a delegate object whose parameter form must be the same as the parameters of the method you want to include.

2. Define all the methods you want to define, and the parameter form must be the same as the parameter form of the delegate object declared in the first step.

3. Creates a delegate object and includes the desired method in the delegate object.

4. Invokes each method contained in the delegate object.

The following C # code shows how to use the four steps above to implement a delegate mechanism:

Using System;
file://Step 1: Declare a Delegate object
public delegate void MyDelegate (string input);

file://Step 2: Define each method with the same parameter form as the delegate object declared in step 1
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);
}
}

file://Step 3: Create a delegate object and include the method above
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;
}
}

file://Step 4: Invoke the method contained in the delegate object
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 handler functions in C #:

The event-handling function in C # is a delegate object in the form of a particular parameter, as follows:

public delegate void MyEventHandler (object sender, MyEventArgs e);

The first parameter (sender) indicates the object that triggered the event, and the second parameter (e) contains some data that can be used in the event handler function. The MyEventArgs class above is inherited from the EventArgs class, which is the base class for some of the more widely used classes, such as the MouseEventArgs class, the ListChangedEventArgs class, and so on. For GUI-based events, you can use these broader, already defined objects of the class to do the processing, and for those that are based on non-GUI, you have to derive your class from the EventArgs class and pass the data you want to the delegate object. The following is a simple example:

public class MyEventArgs eventargs{
public string M_myeventargumentdata;
}

In the event handler function, you can refer to the delegate object by using the keyword event, as follows:

public event MyEventHandler MyEvent;

Now we're going to create two classes, and through these two classes we can see how C # completes the event handling mechanism. In our example, Class A will provide the handler for the event and include the event handler function in step 3 when the delegate object is created, as described above, the argument form of the event handler function must match the parameter form of the delegate object. The Class A then passes the delegate object to Class B. When an event in class B is triggered, the event handler function in Class A is invoked accordingly. Here is the sample code:

Using System;
file://Step 1: Declaring a Delegate object
public delegate void MyHandler1 (Object Sender,myeventargs e);
public delegate void MyHandler2 (Object Sender,myeventargs e);

file://Step 2: How to create an event handler 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 the delegate object, and the event handler function contains the object in which the event will be triggered
Public A (b) {
MyHandler1 d1=new MyHandler1 (OnHandler1);
MyHandler2 d2=new MyHandler2 (OnHandler2);
B.event1 +=d1;
B.event2 +=d2;
}
}

file://Step 4: Invoke the included method through the delegate object (that is, the triggering event)
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 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);
}
}

Event handler functions for the GUI in C #:

The basic method of completing the event-handling function under the GUI is not much different from the one described above, so we can complete a simple instance program through the above method. The main class MyForm class of the instance program inherits from the form class. By looking at the entire code and the relevant annotations, you can see that we have not declared the delegate object and referenced the delegate object through the event keyword, because the GUI control has already done the work for us, and its delegate object is System.EventHandler. However, we still have to define methods for each control (that is, the handler function for the event) and include them in the created delegate object (System.EventHandler). That way, the corresponding event handler function is triggered when the user interacts with the program. The specific code is as follows:


Related Article

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.