Declaration: forwarding from Network
Introduction
C # Language evolved from C/C ++. It is modern, simple, fully object-oriented, and type-safe. C # The language is targeted by Microsoft. A new language launched by the. NET platform. the first language of the. NET platform, which focuses almost all the latest results on software development and software engineering research. Object-oriented, type security, component technology, automatic memory management, cross-platform Exception Handling, version control,CodeSecurity management ......
Applications in. netProgramIn development, whether it is web forms (Asp. net) or Windows Forms, all involve the Event Response and processing of a large number of objects, such as the customer submitting an order online, or moving the mouse over the windows window. In C #, how does one declare an event and add a response method to the event? The followingArticleThis is a detailed description.
Principles
In C #, a class can contain fields, properties, methods, indexes, and events, the events member declares a class event. The following syntax is used to declare an event member in a class:
Public event indicates the name of the event.
For example, if a click event member is declared in the control class, its syntax is as follows:
Public event eventhandler click;
In C #, a new data type delegate (representative) is added to solve the event processing problem. It indicates that the data type is very similar to the pointer in C language. What is different from the pointer is that the Code is secure and manageable. Due to the simplicity of C # itself, it is very easy to understand delegate for programs that have never used C and pointers.
In C #, by using delegate, you can easily use the "+ =" (plus or equal) operator. add one or more response methods to an event in the. NET object. You can also cancel these response methods by using the very simple "-=" (minus or equal) operator. For example, the following statement adds a click event for the temp button:
Temp. Click + = new system. eventhandler (this. Test); // Add an event handling method for test.
In the statement that declares the event above, eventhandler is a delegate (Representative) type, which is declared in the. NET class library as follows:
Public Delegate void eventhandler (Object sender, eventargs E );
In this way, all functions, such as void (object parameter name, eventargs parameter name), can be used as the Click Event Response Method of the control class. As defined below, an event response method:
Private void button#click (Object sender, system. eventargs E)
Because events are handled by Delegate (representing the type), an event may have multiple response methods through accumulation. At the same time, you can also use a method as the response method for multiple events. (Note: Only "+ =" and "-=" can appear after the event member in the C # language class to indicate operators for adding and canceling Event Response functions .)
Whether it is ASP. NET or general Windows Forms programming, in C #, basically the event response methods we encounter are described as follows:
Private void button#click (Object sender, system. eventargs E)
Do the access permissions, return value types, parameters, types, and even method names of an event response method remain unchanged? The answer is: no!
Generally, there are two parameters in the event response method, one of which indicates that the event-triggering object is sender. Because the event-triggering object is unpredictable, therefore, we declare it as the object type, and all objects apply. The second parameter indicates the specific information of the event. Different types of events may be different, which is determined by the description of the event members in the class.
We know that events are handled through Delegate (representative. Assume that the event representative is described as follows:
Delegate int myeventhandler (Object sender, toolbarbuttonclickeventargs E );
When the above Event Response Function declaration is involved, it must be declared as follows:
Private int mytest (Object sender, toolbarbuttonclickeventargs E)
{
}
You can use the following code to add an event response method to an object:
Control. event + = new myeventhandler (mytest );
Sample program
Next, we designed a simple Windows Forms Program with Visual Studio. NET development tool, showing you how to implement Event Response Processing in C.
* Main class
System. Windows. Forms. Application class: Application class.
System. Windows. Forms. Form class: Form class.
System. Windows. Forms. Label: a text label class, used to Add Tag Information in a window.
System. Windows. Forms. Button class: button class, which generates a command to press.
System. eventhandler delegate (Representative): It is.. Net class library, which is mainly used to describe and initialize an event method. This represents an object with two parameters sender, system. eventargs e indicates the event information, such as the x and y values of the mouse. * Design overview
In Visual Studio. net select new windows application, select the program address and enter the program name, a very simple initialization form form1 class will be generated; add a label in the initial form) and a button, and set the corresponding location size and attribute value.
Double-click the button to enter the code editing area. At this time, the system automatically generates a method for processing the button click event, that is, button#click (). Add the following code to the method:
Button temp = new button ();
Temp. Text = "New button ";
Temp. Location = new point (30, 80 );
Temp. Click + = new eventhandler (this. test );
This. Controls. Add (temp );
Label1.click + = new eventhandler (this. test );
After completion, add a method test () for the form form1 class to respond to the event, as shown below:
Private void test (Object sender, system. eventargs E)
{
MessageBox. Show ("this is my custom Event Response Function! "," Prompt information ");
}
After the code is saved, compile and run the code to check the Event Response of each control in the program.
* Running Effect
There is a tag and a button at the beginning of the program running. Click the tag without any response information, and then click "add event for the control, A button displayed as "new button" will be added to the form. Then, click the tag and you will see the response information. A dialog box will pop up to indicate that the operation has been processed. Click "New button" to view the Event Response Information.
Click "Add button for event", and the page will remain unchanged. In fact, there are already two buttons displayed as "new button" on the form, it is only because it is repeatedly displayed in the same position and cannot be seen. It is very interesting to click the tag and try again. We will find that the event response method has been executed twice.
* Key code and comments
Below we list the core code in this program, and make a detailed comment, please carefully understand.
Private Void Initializecomponent () // The form initialization function called by the form constructor is automatically generated by Visual Studio. NET.
{
This . Button1.click + = New System. eventhandler ( This . Button#click ); // Add the response method button1_click () for the Click Event of the button1 object ().
}
Private Void Button#click ( Object Sender, system. eventargs E) // The click event response method of the button1 button generated by the system.
{
Button temp = New Button (); // Generate a Button Object
Temp. Text = " New button " ; // Set the appearance attribute of the button object
Temp. Location = New Point ( 30 , 80 );
Temp. Click + = New Eventhandler ( This . Test ); // Add test () to the new button click event response method.
This . Controls. Add (temp ); // Add the button object temp to the current form
Label1.click + = New Eventhandler ( This . Test ); // Add test () as the response method of the label (label1) Click Event. Note that the event response in C # can contain multiple methods or duplicate methods.
}
Private Void Test ( Object Sender, system. eventargs E) // For custom event processing functions, pay attention to the parameter types of the functions.
{
MessageBox. Show ( " This is my custom Event Response Function! " , " Prompt information " ); // A dialog box is displayed.
{< P>