Events and response methods

Source: Internet
Author: User
IntroductionYan
C #
Language evolved from C/C ++. It is modern, simple, fully object-oriented, and type-safe. C # Language is a new language launched by Microsoft for the. NET platform. As the first language of the. NET platform, it has concentrated 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, Code Security management ......
Applications in. net Program In development, whether it is web forms (ASP. NET) or Windows
Forms involves Event Response and processing of a large number of objects. For example, events occur when a customer submits an order online or moves the mouse over a Windows window. In C # How does one declare an event and add a response method for the event? The following Article This is a detailed description.
Principles
In C # A class can contain fields, properties, methods, indexes, and events) A member is used to declare 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 eventEventhandlerClick;

InC #, Added a new data type delegate (Representative) 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. BecauseC #Its simplicity is very easy for programs that have never used C and pointers.
InC #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,EventhandlerIs a delegate (Representative) type, which is declared in the. NET class library as follows:

Public Delegate voidEventhandler(Object
Sender, eventargs E );

In this way, all parameters, such as: void name (object parameter name, eventargs
Parameter Name); functions 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:C #Only the operators "+ =" and "-=" can appear after the event member in the language class to add and cancel the event response function .)
Whether it is ASP. NET or general Windows Forms programmingC #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 represented by Delegate)
. 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
below, we use Visual Studio. NET development tool designed a simple Windows Forms Program, this article shows 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, which indicates the object that triggers the event, 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 + = newEventhandler(This. test );
This. Controls. Add (temp );
Label1.click + = newEventhandler(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. As shown in:

* 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, which is mainly called by Visual Studio
. Net automatically generated.
{
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"; // sets the appearance attribute of the button object.
Temp. Location = new point (30, 80 );
Temp. Click + = newEventhandler(This. Test); // Add test () to the response method of the Add button click event.
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 C # event responses can have multiple methods or duplicate methods.
}< br> private void test (Object sender, system. eventargs
E) // customize the event processing function. Pay attention to the parameter type of the function.
{< br> MessageBox. Show ("this is my custom Event Response Function! "," Prompt information "); // a dialog box is displayed.
}< WBR>

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.