Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Mouse and Keyboard Events in Windows Forms

Visual Studio .NET 2003

There are a number of events related to the user's employment of the mouse and keyboard. Each of these events has an event handler for which you can write code in your Windows applications. These events include MouseDown, MouseUp, MouseMove, MouseEnter, MouseLeave, MouseHover, KeyPress, KeyDown, and KeyUp. The MouseDown, MouseUp, and MouseMove event handlers receive an argument of MouseEventArgs containing data related to their events. The MouseEnter, MouseLeave, and MouseHover event handlers receive an argument of type EventArgs containing data related to their events. The key-related event handlers receive an argument of type KeyEventArgs containing data related to their events. These events are handled just like any other events that occur on Windows Forms. For more information about handling events, see Introduction to Event Handlers In Windows Forms.

Additionally, be aware that you can change the mouse cursor while operations are occurring. This capability can be paired with the MouseEnter and MouseLeave events to both provide feedback to the user that computations are occurring as well as limit user interaction for specified amounts of time. For example, if you show a dialog box to indicate files are being copied, it is customary to change the cursor to the hourglass.

Sometimes, the mouse cursor will change because of system events, such as when your application is involved in a drag-and-drop operation. For more information about drag-and-drop procedures, see Drag-and-Drop Operations and Clipboard Support. You can change the mouse cursor by setting the Cursor property of a control. For more information about working with cursors, see Cursors Class.

For examples of changing the mouse cursor, see Cursor Class.

See Also

Determining Which Modifier Key Was Pressed | Event Handling in Windows Forms | Introduction to Windows Forms | Windows Forms

© 2015 Microsoft