* Move ellipsis vs button to shared folder * Add control events articles * Update TOC * acro * Updates;redirects * Fix redirect * feedback
7.5 KiB
title, description, ms.date, ms.topic, dev_langs, f1_keywords, helpviewer_keywords
| title | description | ms.date | ms.topic | dev_langs | f1_keywords | helpviewer_keywords | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Control events overview | Learn about the different types of events exposed by controls in Windows Forms for .NET. Controls raise events when the user interacts with the control. | 07/16/2021 | overview |
|
|
|
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.
[!INCLUDE desktop guide under construction]
For more information about how to add or remove a control event handler, see How to handle an event.
Common events
Controls provide a set of common events through the base class: xref:System.Windows.Forms.Control. Not every control responds to every event. For example, the xref:System.Windows.Forms.Label control doesn't respond to keyboard input, so the xref:System.Windows.Forms.Control.PreviewKeyDown?displayProperty=nameWithType event isn't raised. 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:
- xref:System.Windows.Forms.Control.MouseClick
- xref:System.Windows.Forms.Control.MouseDoubleClick
- xref:System.Windows.Forms.Control.MouseDown
- xref:System.Windows.Forms.Control.MouseEnter
- xref:System.Windows.Forms.Control.MouseHover
- xref:System.Windows.Forms.Control.MouseLeave
- xref:System.Windows.Forms.Control.MouseMove
- xref:System.Windows.Forms.Control.MouseUp
- xref:System.Windows.Forms.Control.MouseWheel
- xref:System.Windows.Forms.Control.Click
For more information, see Using mouse events.
Keyboard events
If the control responds to user input, such as a xref:System.Windows.Forms.TextBox or xref:System.Windows.Forms.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 xref:System.Windows.Forms.Label control, can't be focused and can't receive keyboard events. The following is a list of keyboard events:
- xref:System.Windows.Forms.Control.KeyDown
- xref:System.Windows.Forms.Control.KeyPress
- xref:System.Windows.Forms.Control.KeyUp
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:
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 xref:System.Windows.Forms.Control.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 xref:System.Windows.Forms.Control.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.