Bewerken

Delen via


Control events (Windows Forms .NET)

Controls provide events that are raised when the user interacts with the control or when the state of the control changes. This article describes the common events shared by most controls, events raised by user interaction, and events unique to specific controls. For more information about events in Windows Forms, see Events overview and Handling and raising events.

For more information about how to add or remove a control event handler, see How to handle an event.

Common events

Controls provides more than 60 events through the base class Control. These include the Paint event, which causes a control to be drawn, events related to displaying a window, such as the Resize and Layout events, and low-level mouse and keyboard events. Some low-level events are synthesized by Control into semantic events such as Click and DoubleClick. Most shared events fall under these categories:

  • Mouse events
  • Keyboard events
  • Property changed events
  • Other events

Mouse events

Considering Windows Forms is a User Interface (UI) technology, mouse input is the primary way users interact with a Windows Forms application. All controls provide basic mouse-related events:

For more information, see Using mouse events.

Keyboard events

If the control responds to user input, such as a TextBox or Button control, the appropriate input event is raised for the control. The control must be focused to receive keyboard events. Some controls, such as the Label control, can't be focused and can't receive keyboard events. The following is a list of keyboard events:

For more information, see Using keyboard events.

Property changed events

Windows Forms follows the PropertyNameChanged pattern for properties that have change events. The data binding engine provided by Windows Forms recognizes this pattern and integrates well with it. When creating your own controls, implement this pattern.

This pattern implements the following rules, using the property FirstName as an example:

  • Name your property: FirstName.
  • Create an event for the property using the pattern PropertyNameChanged: FirstNameChanged.
  • Create a private or protected method using the pattern OnPropertyNameChanged: OnFirstNameChanged.

If the FirstName property set modifies the backing value, the OnFirstNameChanged method is called. The OnFirstNameChanged method raises the FirstNameChanged event.

Here are some of the common property changed events for a control:

Event Description
BackColorChanged Occurs when the value of the BackColor property changes.
BackgroundImageChanged Occurs when the value of the BackgroundImage property changes.
BindingContextChanged Occurs when the value of the BindingContext property changes.
DockChanged Occurs when the value of the Dock property changes.
EnabledChanged Occurs when the Enabled property value has changed.
FontChanged Occurs when the Font property value changes.
ForeColorChanged Occurs when the ForeColor property value changes.
LocationChanged Occurs when the Location property value has changed.
SizeChanged Occurs when the Size property value changes.
VisibleChanged Occurs when the Visible property value changes.

For a full list of events, see the Events section of the Control Class.

Other events

Controls will also raise events based on the state of the control, or other interactions with the control. For example, the HelpRequested event is raised if the control has focus and the user presses the F1 key. This event is also raised if the user presses the context-sensitive Help button on a form, and then presses the help cursor on the control.

Another example is when a control is changed, moved, or resized, the Paint event is raised. This event provides the developer with the opportunity to draw on the control and change its appearance.

For a full list of events, see the Events section of the Control Class.

See also