C # window event processing

Source: Internet
Author: User

Event Driven is the core of C # window program design. Its importance is equivalent to message ing in VC. If you do not understand Event Driven, you cannot go deep into the window Program Design Hall. In C #, there are two main methods for event processing: The delegate event processing model and the event Method overload.

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" is used 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 the basic class related to events in. NET Framework. All events that occur are encapsulated into eventargs classes or their subclass objects. When an event occurs, method receives the two parameters. Once a delegate 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. The Code is 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. The names of these event methods that can be reloaded start with "on" and are declared as "virtual". A method declared as virtual indicates that it can be reloaded, the method declared as override also indicates that it can be overloaded. If 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 ();

}

}

The above example shows that it is quite easy to process events in C.Author's blog:Http://blog.csdn.net/zhenxizhou/

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.