C # event and response method

xiaoxiao2021-03-06  77

Introduction C # language is evolved from C / C . It is modern, simple, fully objective and type safe. The C # language is a new language that Microsoft is pushed out for the .NET platform, as the first language of the .NET platform, which concentrates on all the latest achievements 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 ... In .NET application development, whether it is Web Forms (ASP.NET) or Windows Forms involved The event response and processing of a large number of objects, such as the customer's online submission, or move the mouse on the Windows window, there will be events. So, how do you declare an event and add a response method for an event? The following article describes this detailed explanation. Principle Introduction In C #, a class can be domain (Fields), Properties, Method, Events, Events, and Events members are used to declare a class event. of. Declaring an event member in a class generally adopted the following grammatical form: public event represents the name of the event name. If a Click event member is declared in the Control class, its syntax is as follows: Public Event athandler click; in C #, a new data type Delegate is added to resolve the event handling problem. The representative data type is very similar to a pointer in the C language, which is different from the pointer, which is a secure, manageable. Due to the simplicity of C # itself, it is also very easy to understand Delegate for programs that have not been used by C and pointers. In C #, by using Delegate, you can add one or even multiple response methods to an event in the .NET object via " =" (add equal) operator; can also be very simple "- =" (Equivalence equivalent) operator cancels these response methods. The statement of the Click event is added as shown below: Temp.Click = New System.EventHandler (THIS.TEST); / / EventHandler is a delegate type, EventHandler is a delegate type, EventHandler is a delegate type. It is declared in the .NET class library: Public Delegate Void EventHandler (Object Sender, Eventargs E); this, all the words such as the Void letter (Object parameter name, Eventargs parameter name); functions can be used as a Control class Click event response method. As defined below: Private Void Button1_Click (Object Sender, System.EventArgs E) Due to the delegate (representative type), it is possible to have multiple response methods by accumulating an event; At the same time, it is also possible to make a method of respond to a plurality of events. (Note: After the EVENT member in the C # language class can only appear " =" and "- =" indicate an operator that adds to the cancel event response function.

Whether it is ASP.NET or average Windows Forms programming, in C #, basically we encounter the event response method to describe the following form: Private void button1_click (Object Sender, System.EventArgs E) So, an event The access rights of the response method, the return value type, the parameters, and the type, and even the method name, etc. must be fixed? The answer is: no! Under normal circumstances, there are two parameters in the response method of the event, one of which represents the object of the incident, the Sender, which is unpredictable, so we declare it as an object type, all objects apply. The second parameter represents the specific information of the incident, which may be different in various types of events, which is determined according to the instructions for event members. We know that the event is processed by delegate. It is assumed to be represented by representing events described in the following form: delegate int MyEventHandler (object sender, ToolBarButtonClickEventArgs e); then when it comes to the above event response function declaration, it must be declared as the following forms: private int MyTest (object sender, ToolBarButtonClickEventArgs e ) {} You can use the following code to implement: control.Event = new myeventhandler (mytest); sample program, we have designed a simple Windows Forms program with Visual Studio .NET development tools, To everyone, how is C # to implement the event response processing. * Major class System.Windows.Forms.Application class: Application class. System.Windows.Forms.Form class: Form class. System.Windows.Forms.Label class: Text tag class, mainly used to add tag information on the window. System.Windows.Forms.Button Class: Buttons, generate a command to press. System.EventHandler Delegate: It is a public representative type in the .NET class library, primarily to explain and initialize an event method, which has two parameters Object Sender indicating the object of the incident, System.EventArgs E representative The corresponding information of the event, such as the X, Y value, etc. of the mouse. * Design Introduction In Visual Studio .NET Select New Windows Application, select the program address and enter the program name, will generate a very simple initial form form1 class; add a tag (Label) and a button in the initial form ( Button and set the corresponding positional size and attribute value. Double-click the button to enter the code editing area. At this time, the system automatically generates a method for handling the button Click event, but button1_click ().

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

New Post(0)