In a program run, there are many bodies that produce events, especially the keyboard and mouse. This article explores the process of handling events related to these two principals in C #.
A This article describes the program design and operation of the software environment:
(1) Microsoft company Windows 2000 Server Edition
(2). Net FrameWork SDK Beta 2
Two Handle mouse-related events in C #:
There are approximately six mouse-related events, respectively:
"MouseHover", "MouseLeave", "MouseEnter", "MouseMove", "MouseDown" and "MouseUp".
(1). How to define these events in a C # program:
In C #, the above events are described by different delegate, where the delegate describing "MouseHover", "MouseLeave", "MouseEnter" Events is "EventHandler", The delegate that describes the three events that follow are described as "MouseEventHandler". These two delegate are encapsulated in different namespaces, where "EventHandler" is encapsulated in the "System" namespace, and "MouseEventHandler" is encapsulated in the "Syetem.Windows.Froms" namespace. The class that passes data for "MouseHover", "MouseLeave", "MouseEnter" Events is "EventArgs" and is encapsulated in the "System" namespace, while the class that provides data for the following three events is " MouseEventArgs, "he was encapsulated in the" Syetem.Windows.Froms "namespace. These determine that there are different approaches to defining these events in C # and responding to them. Here are some of the differences.
For the first three events mentioned above, the following syntax is used to define:
"Component name." Event name "+ = new System.EventHandler (" event name ");
The following is the specific implementation code in the program:
Button1. MouseLeave + = new Syetem.evenhandler (button1_mleave);
After the definition of the event is completed, the code that responds to this event is added to the program, or the program compiles an error. The following is the basic structure that responds to the above events.
private void button1_MLeave ( object sender , System.EventArgs e )
{
此处加入响应此事件的代码
}
The syntax for defining the "MouseMove", "MouseDown", and "MouseUp" events is roughly the same as the three events described earlier:
"Component name." Event name "+ = new System.Windows.Forms. MouseEventHandler ("event name");
The following is the specific implementation code in the program:
button1.MouseMove += new System.Windows.Forms.MouseEventHandler(button1_MMove);
The following is the basic structure in response to the above events:
private void button1_MMove ( object sender , System.Windows.Forms. MouseEventArgs e )
{
此处加入响应此事件的代码
}
Note: the "Button1" in the above program is defined as a button component.