Skip to content

Commit 5d38c60

Browse files
authored
Add control event articles (#1113)
* Move ellipsis vs button to shared folder * Add control events articles * Update TOC * acro * Updates;redirects * Fix redirect * feedback
1 parent d09a701 commit 5d38c60

23 files changed

+852
-6
lines changed

.openpublishing.redirection.json

+12
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,18 @@
423423
{
424424
"source_path": "dotnet-desktop-guide/net/winforms/controls/how-to-dock-controls-on-windows-forms.md",
425425
"redirect_url": "/dotnet/desktop/winforms/controls/how-to-dock-and-anchor?view=netdesktop-5.0"
426+
},
427+
{
428+
"source_path": "dotnet-desktop-guide/net/winforms/how-to-create-event-handlers-at-run-time-for-windows-forms.md",
429+
"redirect_url": "/dotnet/desktop/winforms/controls/how-to-add-an-event-handler?view=netdesktop-5.0"
430+
},
431+
{
432+
"source_path": "dotnet-desktop-guide/framework/winforms/controls/how-to-add-an-event-handler.md",
433+
"redirect_url": "/dotnet/desktop/winforms/how-to-create-event-handlers-at-run-time-for-windows-forms?view=netframeworkdesktop-4.8"
434+
},
435+
{
436+
"source_path": "dotnet-desktop-guide/net/winforms/how-to-connect-multiple-events-to-a-single-event-handler-in-windows-forms.md",
437+
"redirect_url": "/dotnet/desktop/winforms/controls/how-to-add-an-event-handler?view=netdesktop-5.0#how-to-use-multiple-events-with-the-same-handler"
426438
}
427439
]
428440
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
title: Control events overview
3+
description: 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.
4+
ms.date: 07/16/2021
5+
ms.topic: overview
6+
dev_langs:
7+
- "csharp"
8+
- "vb"
9+
f1_keywords:
10+
- "OnPaint"
11+
helpviewer_keywords:
12+
- "Windows Forms, event handling"
13+
- "events [Windows Forms], connecting multiple to single event handler"
14+
- "event handlers [Windows Forms], connecting events to"
15+
- "menus [Windows Forms], event-handling methods for multiple menu items"
16+
- "Windows Forms controls, events"
17+
- "menu items [Windows Forms], multicasting event-handling methods"
18+
---
19+
20+
# Control events (Windows Forms .NET)
21+
22+
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](../forms/events.md) and [Handling and raising events](/dotnet/standard/events/index).
23+
24+
[!INCLUDE [desktop guide under construction](../../includes/desktop-guide-preview-note.md)]
25+
26+
For more information about how to add or remove a control event handler, see [How to handle an event](how-to-add-an-event-handler.md).
27+
28+
## Common events
29+
30+
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:
31+
32+
- Mouse events
33+
- Keyboard events
34+
- Property changed events
35+
- Other events
36+
37+
## Mouse events
38+
39+
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:
40+
41+
- <xref:System.Windows.Forms.Control.MouseClick>
42+
- <xref:System.Windows.Forms.Control.MouseDoubleClick>
43+
- <xref:System.Windows.Forms.Control.MouseDown>
44+
- <xref:System.Windows.Forms.Control.MouseEnter>
45+
- <xref:System.Windows.Forms.Control.MouseHover>
46+
- <xref:System.Windows.Forms.Control.MouseLeave>
47+
- <xref:System.Windows.Forms.Control.MouseMove>
48+
- <xref:System.Windows.Forms.Control.MouseUp>
49+
- <xref:System.Windows.Forms.Control.MouseWheel>
50+
- <xref:System.Windows.Forms.Control.Click>
51+
52+
For more information, see [Using mouse events](../input-mouse/events.md).
53+
54+
## Keyboard events
55+
56+
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:
57+
58+
- <xref:System.Windows.Forms.Control.KeyDown>
59+
- <xref:System.Windows.Forms.Control.KeyPress>
60+
- <xref:System.Windows.Forms.Control.KeyUp>
61+
62+
For more information, see [Using keyboard events](../input-keyboard/events.md).
63+
64+
## Property changed events
65+
66+
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.
67+
68+
This pattern implements the following rules, using the property `FirstName` as an example:
69+
70+
- Name your property: `FirstName`.
71+
- Create an event for the property using the pattern `PropertyNameChanged`: `FirstNameChanged`.
72+
- Create a private or protected method using the pattern `OnPropertyNameChanged`: `OnFirstNameChanged`.
73+
74+
If the `FirstName` property set modifies the backing value, the `OnFirstNameChanged` method is called. The `OnFirstNameChanged` method raises the `FirstNameChanged` event.
75+
76+
Here are some of the common property changed events for a control:
77+
78+
| Event | Description |
79+
|------------------------------------------------------------|-------------------------------------------------------------------------------------------------|
80+
| <xref:System.Windows.Forms.Control.BackColorChanged> | Occurs when the value of the <xref:System.Windows.Forms.Control.BackColor%2A> property changes. |
81+
| <xref:System.Windows.Forms.Control.BackgroundImageChanged> | Occurs when the value of the <xref:System.Windows.Forms.Control.BackgroundImage> property changes. |
82+
| <xref:System.Windows.Forms.Control.BindingContextChanged> | Occurs when the value of the <xref:System.Windows.Forms.Control.BindingContext> property changes. |
83+
| <xref:System.Windows.Forms.Control.DockChanged> | Occurs when the value of the <xref:System.Windows.Forms.Control.Dock> property changes. |
84+
| <xref:System.Windows.Forms.Control.EnabledChanged> | Occurs when the <xref:System.Windows.Forms.Control.Enabled> property value has changed. |
85+
| <xref:System.Windows.Forms.Control.FontChanged> | Occurs when the <xref:System.Windows.Forms.Control.Font> property value changes. |
86+
| <xref:System.Windows.Forms.Control.ForeColorChanged> | Occurs when the <xref:System.Windows.Forms.Control.ForeColor> property value changes. |
87+
| <xref:System.Windows.Forms.Control.LocationChanged> | Occurs when the <xref:System.Windows.Forms.Control.Location> property value has changed. |
88+
| <xref:System.Windows.Forms.Control.SizeChanged> | Occurs when the <xref:System.Windows.Forms.Control.Size> property value changes. |
89+
| <xref:System.Windows.Forms.Control.VisibleChanged> | Occurs when the <xref:System.Windows.Forms.Control.Visible> property value changes. |
90+
91+
For a full list of events, see the **Events** section of the [Control Class](xref:System.Windows.Forms.Control#events).
92+
93+
## Other events
94+
95+
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 <kbd>F1</kbd> 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.
96+
97+
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.
98+
99+
For a full list of events, see the **Events** section of the [Control Class](xref:System.Windows.Forms.Control#events).
100+
101+
## See also
102+
103+
- [How to handle an event](how-to-add-an-event-handler.md)
104+
- [Events overview](../forms/events.md)
105+
- [Using mouse events](../input-mouse/events.md)
106+
- [Using keyboard events](../input-keyboard/events.md)
107+
- <xref:System.Windows.Forms.Control?displayProperty=fullName>
108+
- <xref:System.Windows.Forms.Control.Click?displayProperty=fullName>
109+
- <xref:System.Windows.Forms.Button?displayProperty=fullName>

dotnet-desktop-guide/net/winforms/controls/how-to-add-a-picture-to-a-control.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@ Several Windows Forms controls can display images. These images can be icons tha
1919

2020
[!INCLUDE [desktop guide under construction](../../includes/desktop-guide-preview-note.md)]
2121

22-
## Designer
22+
## Display an image - designer
2323

24-
In the **Properties** window of Visual Studio, select the **Image** or **BackgroundImage** property of the control, and then select the ellipsis (![Ellipsis button in Visual Studio](../media/visual-studio-ellipsis-button.png)) to display the **Select Resource** dialog box and then select the image you want to display.
24+
In Visual Studio, use the Visual Designer to display an image.
2525

26-
:::image type="content" source="media/how-to-add-a-picture-to-a-control/properties-image.png" alt-text="Properties dialog with image property selected":::
26+
01. Open the Visual Designer of the form containing the control to change.
27+
01. Select the control.
28+
01. In the **Properties** pane, select the **Image** or **BackgroundImage** property of the control.
29+
01. Select the ellipsis (:::image type="icon" source="../media/shared/visual-studio-ellipsis-button.png" border="false":::) to display the **Select Resource** dialog box and then select the image you want to display.
2730

28-
## Programmatic
31+
:::image type="content" source="media/how-to-add-a-picture-to-a-control/properties-image.png" alt-text="Properties dialog with image property selected":::
2932

30-
Set the control's `Image` or `BackgroundImage` property to an object of type <xref:System.Drawing.Image>. Generally, you'll be loading the image from a file by using the <xref:System.Drawing.Image.FromFile%2A> method.
33+
## Display an image - code
34+
35+
Set the control's `Image` or `BackgroundImage` property to an object of type <xref:System.Drawing.Image>. Generally, you'll load the image from a file by using the <xref:System.Drawing.Image.FromFile%2A> method.
3136

3237
In the following code example, the path set for the location of the image is the **My Pictures** folder. Most computers running the Windows operating system include this directory. This also enables users with minimal system access levels to run the application safely. The following code example requires that you already have a form with a <xref:System.Windows.Forms.PictureBox> control added.
3338

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
---
2+
title: How to add or remove an event handler
3+
description: Learn how to create an event handler at design-time with the Windows Forms Designer in Visual Studio or at run-time.
4+
ms.date: 07/16/2021
5+
dev_langs:
6+
- "csharp"
7+
- "vb"
8+
helpviewer_keywords:
9+
- "Windows Forms, event handling"
10+
- "event handlers [Windows Forms], creating"
11+
- "run time [Windows Forms], creating event handlers at"
12+
- "examples [Windows Forms], event handling"
13+
- "Button control [Windows Forms], event handlers"
14+
- "events [Windows Forms], connecting multiple to single event handler"
15+
- "event handlers [Windows Forms], connecting events to"
16+
- "menus [Windows Forms], event-handling methods for multiple menu items"
17+
- "Windows Forms controls, events"
18+
- "menu items [Windows Forms], multicasting event-handling methods"
19+
---
20+
21+
# How to handle an event (Windows Forms .NET)
22+
23+
Events for controls (and for forms) are generally set through the Visual Studio Visual Designer for Windows Forms. Setting an event through the Visual Designer is known as handling an event at design-time. You can also handle events dynamically in code, known as handling events at run-time. An event created at run-time allows you to connect event handlers dynamically based on what your app is currently doing.
24+
25+
[!INCLUDE [desktop guide under construction](../../includes/desktop-guide-preview-note.md)]
26+
27+
## Handle an event - designer
28+
29+
In Visual Studio, use the Visual Designer to manage handlers for control events. The Visual Designer will generate the handler code and add it to the event for you.
30+
31+
### Set the handler
32+
33+
Use the **Properties** pane to add or set the handler of an event:
34+
35+
01. Open the Visual Designer of the form containing the control to change.
36+
01. Select the control.
37+
01. Change the **Properties** pane mode to **Events** by pressing the events button (:::image type="icon" source="../media/shared/visual-studio-events-button.png" border="false":::).
38+
01. Find the event you want to add a handler to, for example, the **Click** event:
39+
40+
:::image type="content" source="media/how-to-add-an-event-handler/visual-studio-properties-events-click.png" alt-text="Visual Studio properties pane shown with the events mode enabled and the click event.":::
41+
42+
01. Do one of the following:
43+
44+
- Double-click the event to generate a new handler, it's blank if no handler is assigned. If it's not blank, this action opens the code for the form and navigates to the existing handler.
45+
46+
- Use the selection box (:::image type="icon" source="../media/shared/visual-studio-chevron-button.png" border="false":::) to choose an existing handler.
47+
48+
The selection box will list all methods that have a compatible method signature for the event handler.
49+
50+
### Clear the handler
51+
52+
To remove an event handler, you can't just delete handler code that is in the form's code-behind file, it's still referenced by the event. Use the **Properties** pane to remove the handler of an event:
53+
54+
01. Open the Visual Designer of the form containing the control to change.
55+
01. Select the control.
56+
01. Change the **Properties** pane mode to **Events** by pressing the events button (:::image type="icon" source="../media/shared/visual-studio-events-button.png" border="false":::).
57+
01. Find the event containing the handler you want to remove, for example, the **Click** event:
58+
59+
:::image type="content" source="media/how-to-add-an-event-handler/visual-studio-properties-events-click.png" alt-text="Visual Studio properties pane shown with the events mode enabled and the click event.":::
60+
61+
01. Right-click on the event and choose **Reset**.
62+
63+
## Handle an event - code
64+
65+
You typically add event handlers to controls at design-time through the Visual Designer. You can, though, create controls at run-time, which requires you to add event handlers in code. Adding handlers in code also gives you the chance to add multiple handlers to the same event.
66+
67+
### Add a handler
68+
69+
The following example shows how to create a control and add an event handler. This control is created in the [`Button.Click`](xref:System.Windows.Forms.Control.Click) event handler a different button. When **Button1** is pressed. The code moves and sizes a new button. The new button's `Click` event is handled by the `MyNewButton_Click` method. To get the new button to appear, it's added to the form's `Controls` collection. There's also code to remove the `Button1.Click` event's handler, this is discussed in the [Remove the handler](#remove-the-handler) section.
70+
71+
:::code language="csharp" source="snippets/how-to-add-an-event-handler/cs/Form1.cs" id="HandlerViaCode" highlight="12":::
72+
:::code language="vb" source="snippets/how-to-add-an-event-handler/vb/Form1.vb" id="HandlerViaCode" highlight="8":::
73+
74+
To run this code, do the following to a form with the Visual Studio Visual Designer:
75+
76+
01. Add a new button to the form and name it **Button1**.
77+
01. Change the **Properties** pane mode to **Events** by pressing the event button (:::image type="icon" source="../media/shared/visual-studio-events-button.png" border="false":::).
78+
01. Double-click the **Click** event to generate a handler. This action opens the code window and generates a blank `Button1_Click` method.
79+
01. Replace the method code with the previous code above.
80+
81+
For more information about C# events, see [Events (C#)](/dotnet/csharp/programming-guide/events/)
82+
For more information about Visual Basic events, see [Events (Visual Basic)](/dotnet/visual-basic/programming-guide/language-features/events/)
83+
84+
### Remove the handler
85+
86+
The [Add a handler](#add-a-handler) section used some code to demonstrate adding a handler. That code also contained a call to remove a handler:
87+
88+
:::code language="csharp" source="snippets/how-to-add-an-event-handler/cs/Form1.cs" id="RemoveHandler":::
89+
:::code language="vb" source="snippets/how-to-add-an-event-handler/vb/Form1.vb" id="RemoveHandler":::
90+
91+
This syntax can be used to remove any event handler from any event.
92+
93+
For more information about C# events, see [Events (C#)](/dotnet/csharp/programming-guide/events/)
94+
For more information about Visual Basic events, see [Events (Visual Basic)](/dotnet/visual-basic/programming-guide/language-features/events/)
95+
96+
## How to use multiple events with the same handler
97+
98+
With the Visual Studio Visual Designer's **Properties** pane, you can select the same handler already in use by a different event. Follow the directions in the [Set the handler](#set-the-handler) section to select an existing handler instead of creating a new one.
99+
100+
In C#, the handler is attached to a control's event in the form's designer code, which changed through the Visual Designer. For more information about C# events, see [Events (C#)](/dotnet/csharp/programming-guide/events/)
101+
102+
### Visual Basic
103+
104+
In Visual Basic, the handler is attached to a control's event in the form's code-behind file, where the event handler code is declared. Multiple `Handles` keywords can be added to the event handler code to use it with multiple events. The Visual Designer will generate the `Handles` keyword for you and add it to the event handler. However, you can easily do this yourself to any control's event and event handler, as long as the signature of the handler method matches the event. For more information about Visual Basic events, see [Events (Visual Basic)](/dotnet/visual-basic/programming-guide/language-features/events/)
105+
106+
This code demonstrates how the same method can be used as a handler for two different [`Button.Click`](xref:System.Windows.Forms.Control.Click) events:
107+
108+
:::code language="vb" source="snippets/how-to-add-an-event-handler/vb/Form2.vb" id="MultipleHandlers":::
109+
110+
## See also
111+
112+
- [Control events](events.md)
113+
- [Events overview](../forms/events.md)
114+
- [Using mouse events](../input-mouse/events.md)
115+
- [Using keyboard events](../input-keyboard/events.md)
116+
- <xref:System.Windows.Forms.Button?displayProperty=fullName>

dotnet-desktop-guide/net/winforms/controls/how-to-set-the-display-text.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ You can also set the text by using the [designer](#designer).
3737

3838
:::image type="content" source="media/how-to-set-the-text-displayed-by-a-windows-forms-control/properties-text.png" alt-text="Visual Studio Properties pane for .NET Windows Forms with Text property shown.":::
3939

40-
01. In the **Properties** window, select the ellipsis button (![Ellipsis button (...) in the Properties window of Visual Studio](../media/visual-studio-ellipsis-button.png)) next to the **Font** property.
40+
01. In the **Properties** window, select the ellipsis button (:::image type="icon" source="../media/shared/visual-studio-ellipsis-button.png" border="false":::) next to the **Font** property.
4141

4242
:::image type="content" source="media/how-to-set-the-text-displayed-by-a-windows-forms-control/properties-font.png" alt-text="Visual Studio Properties pane for .NET Windows Forms with Font property shown.":::
4343

0 commit comments

Comments
 (0)