Handling Multiple Events
C#.NET is an event-driven language, which means the objects of C#.NET reacts when an event is triggered, for example, clicking a button and entering some text into a text box.
An event is nothing but an action that calls a function or may cause another event, which does something as specified in the code. You can control the flow of an application by Handling Multiple Events.
There are mainly two types of Handling Multiple Events: Mouse events (events generated by supplying the input through a mouse) and Keyboard events (events generated by supplying the input through the keyboard).
You can handle mouse events-such as mouse movements in forms and controls. Following are the possible mouse events for the Control class, which is a base class for controls and forms:
- MouseDown: Occurs when the mouse pointer is over the control and a mouse button pressed
- MouseEnter: Occurs when the mouse pointer enters the control
- MouseHover: Occurs when the mouse pointer moves to and from over the control
- MouseLeave: Occurs when the mouse pointer leaves the control
- MouseMove: Occurs when the mouse pointer moves over the control
- Mouseup: Occurs when the mouse pointer is over the control and a mouse button released
- MouseWheel: Occurs when the mouse wheel moves, while the control has focus
Properties of Handling Multiple Events
The following properties of the MouseEventArgs object are used to handle the mouse events
- Buttons: Indicates which mouse button pressed.
- Clicks: Indicates the number of times the mouse button is pressed and released.
- Delta: Indicates a calculated count of the number of detents the mouse wheel has rotated. A detent is the rotation of the mouse wheel by one notch.
- X: Indicates the x-coordinate of a mouse click.
- Y: Indicates the у-coordinate of a mouse click.
The Buttons property holds one of these members of the MouseButtons enumeration
- Left: Indicates that the left mouse button pressed
- Middle: Indicates that the middle mouse button pressed
- None: Indicates that no mouse button pressed
- Right: Indicates that the right mouse button pressed
- XButton1: Indicates that the first XButton pressed
- XButton2: Indicates that the second XButton pressed
In C#.NET, the following events are available to handle the keyboard events:
- KeyDown: Occurs when a key pressed down while the control has focus
- Keypress: Occurs when a key pressed while the control has focus
- KeyUp: Occurs when a key released while the control has focus
For the Key Down and KeyUp events, the event handler receives an argument of type KeyEventArgs, containing data related to this event with these properties:
- Alt: Holds a value indicating whether the ALT key pressed
- Control: Holds a value indicating whether the CTRL key pressed
- Handled: Holds or sets a value indicating whether the event handled
- KeyCode: Holds the keyboard code for a KeyDown or KeyUp event
- KeyData: Holds the keyboard data for a KeyDown or KeyUp event
- KeyValue: Holds the keyboard value for a KeyDown or KeyUp event
- Modifiers: Holds the modifier flags for a KeyDown or KeyUp event. This indicates which modifier keys (Ctrl, Shift, and/or Alt) pressed.
- Shift: Holds a value indicating whether the shift key pressed
To handle the Keypress events, event handlers take an argument of type KeyPressEventArgs, which contains the following properties:
- Handled: Gets or sets a value indicating whether the Keypress event handled. If you set this value to True, Visual Basic will not handle this key.
- KeyChar: Holds the character corresponding to the key pressed