다음을 통해 공유


Mouse Events in Windows Forms

When you handle mouse input, you usually want to know the location of the mouse pointer and the state of the mouse buttons. This topic provides details on how to get this information from mouse events, and explains the order in which mouse click events are raised in Windows Forms controls. For a list and description of all of the mouse events, see How Mouse Input Works in Windows Forms. Event Handlers Overview (Windows Forms)
Events Overview (Windows Forms)
Event Handlers Overview (Windows Forms)
Events Overview (Windows Forms)
Event Handlers Overview (Windows Forms)
Events Overview (Windows Forms)
Event Handlers Overview (Windows Forms)
Events Overview (Windows Forms)

Mouse Information

A MouseEventArgs is sent to the handlers of mouse events related to clicking a mouse button and tracking mouse movements. MouseEventArgs provides information about the current state of the mouse, including the location of the mouse pointer in client coordinates, which mouse buttons are pressed, and whether the mouse wheel has scrolled. Several mouse events, such as those that simply notify when the mouse pointer has entered or left the bounds of a control, send an EventArgs to the event handler with no further information.

If you want to know the current state of the mouse buttons or the location of the mouse pointer, and you want to avoid handling a mouse event, you can also use the MouseButtons and MousePosition properties of the Control class. MouseButtons returns information about which mouse buttons are currently pressed. The MousePosition returns the screen coordinates of the mouse pointer and is equivalent to the value returned by Position.

Converting Between Screen and Client Coordinates

Because some mouse location information is in client coordinates and some is in screen coordinates, you may need to convert a point from one coordinate system to the other. You can do this easily by using the PointToClient and PointToScreen methods available on the Control class.

Standard Click Event Behavior

If you want to handle mouse click events in the proper order, you need to know the order in which click events are raised in Windows Forms controls. All Windows Forms controls raise click events in the same order when a mouse button is pressed and released (regardless of which mouse button), except where noted in the following list for individual controls. The following list shows the order of events raised for a single mouse-button click:

  1. MouseDown event.

  2. Click event.

  3. MouseClick event.

  4. MouseUp event.

Following is the order of events raised for a double mouse-button click:

  1. MouseDown event.

  2. Click event.

  3. MouseClick event.

  4. MouseUp event.

  5. MouseDown event.

  6. DoubleClick event. (This can vary, depending on whether the control in question has the StandardDoubleClick style bit set to true. For more information about how to set a ControlStyles bit, see the SetStyle method.)

  7. MouseDoubleClick event.

  8. MouseUp event.

For a code example that demonstrates the order of the mouse click events, see How to: Handle User Input Events in Windows Forms Controls.

Individual Controls

The following controls do not conform to the standard mouse click event behavior:

  • Button, CheckBox, ComboBox, and RadioButton controls

    NoteNote

    For the ComboBox control, the event behavior detailed later occurs if the user clicks on the edit field, the button, or on an item within the list.

    • Left click: Click, MouseClick

    • Right click: No click events raised

    • Left double-click: Click, MouseClick; Click, MouseClick

    • Right double-click: No click events raised

  • TextBox, RichTextBox, ListBox, MaskedTextBox, and CheckedListBox controls

    NoteNote

    The event behavior detailed later occurs when the user clicks anywhere within these controls.

    • Left click: Click, MouseClick

    • Right click: No click events raised

    • Left double-click: Click, MouseClick, DoubleClick, MouseDoubleClick

    • Right double-click: No click events raised

  • ListView control

    NoteNote

    The event behavior detailed later occurs only when the user clicks on the items in the ListView control. No events are raised for clicks anywhere else on the control. In addition to the events described later, there are the BeforeLabelEdit and AfterLabelEdit events, which may be of interest to you if you want to use validation with the ListView control.

    • Left click: Click, MouseClick

    • Right click: Click, MouseClick

    • Left double-click: Click, MouseClick; DoubleClick, MouseDoubleClick

    • Right double-click: Click, MouseClick; DoubleClick, MouseDoubleClick

  • TreeView control

    NoteNote

    The event behavior detailed later occurs only when the user clicks on the items themselves or to the right of the items in the TreeView control. No events are raised for clicks anywhere else on the control. In addition to those described later, there are the BeforeCheck, BeforeSelect, BeforeLabelEdit, AfterSelect, AfterCheck, and AfterLabelEdit events, which may be of interest to you if you want to use validation with the TreeView control.

    • Left click: Click, MouseClick

    • Right click: Click, MouseClick

    • Left double-click: Click, MouseClick; DoubleClick, MouseDoubleClick

    • Right double-click: Click, MouseClick; DoubleClick, MouseDoubleClick

Painting Behavior of Toggle Controls

Toggle controls, such as the controls deriving from the ButtonBase class, have the following distinctive painting behavior in combination with mouse click events:

  1. The user presses the mouse button.

  2. The control paints in the pressed state.

  3. The MouseDown event is raised.

  4. The user releases the mouse button.

  5. The control paints in the raised state.

  6. The Click event is raised.

  7. The MouseClick event is raised.

  8. The MouseUp event is raised.

    NoteNote

    If the user moves the pointer out of the toggle control while the mouse button is down (such as moving the mouse off the Button control while it is pressed), the toggle control will paint in the raised state and only the MouseUp event occurs. The Click or MouseClick events will not occur in this situation.

See Also

Other Resources

Mouse Input in a Windows Forms Application