Export (0) Print
Expand All

Handling User Input

This topic describes the main keyboard and mouse events provided by System.Windows.Forms.Control. When handling an event, control authors should override the protected OnEventName method rather than attaching a delegate to the event. For a review of events, see Events.

Note   If there is no data associated with an event, an instance of the base class EventArgs is passed as an argument to the OnEventName method.

Keyboard Events

The common keyboard events that your control can handle are KeyDown, KeyPress, and KeyUp.

Event NameMethod to OverrideDescription of Event
KeyDownvoid OnKeyDown(KeyEventArgs)Raised only when a key is initially pressed.
KeyPressvoid OnKeyPress

(KeyPressEventArgs)

Raised every time a key is pressed. If a key is held down, a KeyPress event is raised at the repeat rate defined by the operating system.
KeyUpvoid OnKeyUp(KeyEventArgs)Raised when a key is released.
Note   Handling keyboard input is considerably more complex than overriding the events in the preceding table and is beyond the scope of this topic.

Mouse Events

The mouse events that your control can handle are MouseDown, MouseEnter, MouseHover, MouseLeave, MouseMove, and MouseUp.

Event NameMethod to OverrideDescription of Event
MouseDownvoid OnMouseDown(MouseEventArgs)Raised when the mouse button is pressed while the pointer is over the control.
MouseEntervoid OnMouseEnter(EventArgs)Raised when the pointer first enters the region of the control.
MouseHovervoid OnMouseHover(EventArgs)Raised when the pointer hovers over the control.
MouseLeavevoid OnMouseLeave(EventArgs)Raised when the pointer leaves the region of the control.
MouseMovevoid OnMouseMove(MouseEventArgs)Raised when the pointer moves in the region of the control.
MouseUpvoid OnMouseUp(MouseEventArgs)Raised when the mouse button is released while the pointer is over the control or the pointer leaves the region of the control.

The following code fragment shows an example of overriding the MouseDown event.

Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
   MyBase.OnMouseDown(e)
   If Not allowUserEdit Then
      Return
   End If
   Capture = True
   dragging = True
   SetDragValue(New Point(e.X, e.Y))
End Sub
[C#]
protected override void OnMouseDown(MouseEventArgs e) {
   base.OnMouseDown(e);
   if (!allowUserEdit) {
      return;
   }
   Capture = true;
   dragging = true;
   SetDragValue(new Point(e.X, e.Y));
}

The following code fragment shows an example of overriding the MouseMove event.

Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
   MyBase.OnMouseMove(e)
   If Not allowUserEdit Or Not dragging Then
      Return
   End If
   SetDragValue(New Point(e.X, e.Y))
End Sub
[C#]
protected override void OnMouseMove(MouseEventArgs e) {
   base.OnMouseMove(e);
   if (!allowUserEdit || !dragging) {
       return;
   }
   SetDragValue(new Point(e.X, e.Y));
}

The following code fragment shows an example of overriding the MouseUp event.

Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
   MyBase.OnMouseUp(e)
   If Not allowUserEdit Or Not dragging Then
      Return
   End If
   Capture = False
   dragging = False
   value = dragValue
   OnValueChanged(EventArgs.Empty)
End Sub
[C#]
protected override void OnMouseUp(MouseEventArgs e) {
   base.OnMouseUp(e);
   if (!allowUserEdit || !dragging) {
      return;
   }
   Capture = false;
   dragging = false;
   value = dragValue;
   OnValueChanged(EventArgs.Empty);
}

For the complete source code for the FlashTrackBar sample, see Windows Forms Control Sample.

See Also

Events in Windows Forms Controls | Defining an Event | Handling and Raising Events

Show:
© 2014 Microsoft