Probe into the C # window event processing
Event Driven is the core of the C # window program design. Its importance is equivalent to message mapping in the VC, and does not understand the event driver, it is unable to go deep into the hall design. In the C # language, there are two main methods for the process of the event: Delegation Event Model is overloaded with the overloaded method (Event Method).
1, delegate event processing
The delegate event processing mode, as the name suggests, is to delegate a particular event to the method responsible for the event processing.
In .NET, the class has a data member (Data MEMBER) and a Function Member. In addition, there is an "event member" to process event-driven processing.
Event members are those related to this class. It defines what related events occur in an object. Different classes can have different event members.
Event processing can be done by a delegate relationship. The entrustment relationship can be created:
Object.event = new eventhandle (Method);
Its meaning is: If an EVENT event occurs in the object Object, it will be handled by the method Method. An object can entrust multiple different processing, " =" is used to increase new delegate relationships. Method is a place that truly event processing, format:
Public Void Method (Object Sender, Eventargs E);
Eventargs is the basic class of the event in .NET Framework, all events occurred in all events being packaged into objects of the Eventargs class or their subclasses. When the event occurs, Method will receive these two parameters. Once a delegate relationship is established, the system automatically occurs when a specific event occurs, and transfers the event handling method you entrusted.
Let's take a simple example, first generate a window when running, when you click the button on the window, the program ends. code show as below:
Using system;
Using system.windows.forms;
Class Form1: Form
{
Public static void
Main
()
{
Application.run (New Form1 ());
}
Public form1 (): base ()
{
TEXT = "Event Processing Example";
Button button1 = new button ();
Button1.text = "click";
// delegate the Click event of Button1 to CountClick method
Button1.click = New EventHandler (this.countclick);
// Add Button1 to the window.
Controls.add (Button1);
}
Public void CountClick (Object Sender, Eventargs E)
{
Application.exit ();
}
}
2, heavy duty event method
In C #, the events related to different classes are defined except that the event is defined, and the over-duty event method is defined, and the corresponding event processing you want can design by overloading these event methods. These overloaded event methods are started with "on", and they are declared as "Virtual", a method that is declared as Virtual, representing it can be overloaded, the method of declaring Override also represents it can be Overload, if the declaration is Abstract, it must be overloaded. For example, the Click event of the button has a corresponding overloaded ONCLICK event method.
Still in the example above, the code is as follows:
Using system;
Using system.windows.form; class button1: button
{
Public static void
Main
()
{
Form form1 = new form ();
FORM1.TEXT = "Event Processing Example";
Button1 button1 = new button1 ();
Button1.text = "click";
Form1.controls.add (button1);
Application.run (Form1);
}
Protected Override Void Onclick (Eventargs E)
{
Application.exit ();
}
}
Through the above example, event processing is quite simple in C #.