Event
Driven) is C # window Program The core of the design, its importance is equivalent to the Message ing in VC. If you do not understand the event-driven architecture, you cannot go deep into the window Program Design Hall. In C #,
There are two methods: the delegate event processing mode (delegation event model) and the overload of the event method.
1. Handle delegated events
The delegated event processing mode, as its name implies, is to delegate a specific event to a method responsible for event processing.
In. net, classes include data members and function members ). In addition
"Event member" for event-driven processing.
The event member is the event related to the object of this class. It defines the events related to an object. Different classes can have different event members.
Event processing can be performed by delegation. The delegation relationship can be established as follows:
Object. event + = new
Eventhandle (method );
It means that if an event occurs to the object, it is handled by the method. An object can be delegated to multiple different processes. "+ =" is used to add new delegate relationships.
Method is the place where the event is actually processed, in the format:
Public void method (Object
Sender, eventargs E );
Eventargs is. net
Framework, all events are encapsulated into eventargs class or its subclass objects. When an event occurs, method receives the two parameters. I
Once the delegated relationship is established, the system automatically executes the event handling method you delegate when a specific event occurs.
The following is a simple example. A window is generated during runtime. When you click the button in the window, the program ends. Code As follows: 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 the 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. Overload event Methods
In C #, in addition to defining event members for events related to different classes, the event methods that can be reloaded are also defined, you can design the event processing you want by reloading these event methods. These can be reloaded
The event method names start with "on" and are declared as "virtual". A method declared as virtual represents a method that can be overloaded and declared as override.
It can also be overloaded. If it is declared as abstract, it must be overloaded. For example, the Click Event of a button has a corresponding onclick event method that can be reloaded.
The Code is as follows: Using System;
Using System. Windows. forms;
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 ();
}
}
Self: http://www.devedu.com/2005-11-7/12590/default.aspx