mirror of
https://github.com/Stone-Red-Code/docs-desktop.git
synced 2026-09-04 09:06:04 +02:00
Add control event articles (#1113)
* Move ellipsis vs button to shared folder * Add control events articles * Update TOC * acro * Updates;redirects * Fix redirect * feedback
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
---
|
||||
title: Control events overview
|
||||
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.
|
||||
ms.date: 07/16/2021
|
||||
ms.topic: overview
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
f1_keywords:
|
||||
- "OnPaint"
|
||||
helpviewer_keywords:
|
||||
- "Windows Forms, event handling"
|
||||
- "events [Windows Forms], connecting multiple to single event handler"
|
||||
- "event handlers [Windows Forms], connecting events to"
|
||||
- "menus [Windows Forms], event-handling methods for multiple menu items"
|
||||
- "Windows Forms controls, events"
|
||||
- "menu items [Windows Forms], multicasting event-handling methods"
|
||||
---
|
||||
|
||||
# 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](../forms/events.md) and [Handling and raising events](/dotnet/standard/events/index).
|
||||
|
||||
[!INCLUDE [desktop guide under construction](../../includes/desktop-guide-preview-note.md)]
|
||||
|
||||
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).
|
||||
|
||||
## 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](../input-mouse/events.md).
|
||||
|
||||
## 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](../input-keyboard/events.md).
|
||||
|
||||
## 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 |
|
||||
|------------------------------------------------------------|-------------------------------------------------------------------------------------------------|
|
||||
| <xref:System.Windows.Forms.Control.BackColorChanged> | Occurs when the value of the <xref:System.Windows.Forms.Control.BackColor%2A> property changes. |
|
||||
| <xref:System.Windows.Forms.Control.BackgroundImageChanged> | Occurs when the value of the <xref:System.Windows.Forms.Control.BackgroundImage> property changes. |
|
||||
| <xref:System.Windows.Forms.Control.BindingContextChanged> | Occurs when the value of the <xref:System.Windows.Forms.Control.BindingContext> property changes. |
|
||||
| <xref:System.Windows.Forms.Control.DockChanged> | Occurs when the value of the <xref:System.Windows.Forms.Control.Dock> property changes. |
|
||||
| <xref:System.Windows.Forms.Control.EnabledChanged> | Occurs when the <xref:System.Windows.Forms.Control.Enabled> property value has changed. |
|
||||
| <xref:System.Windows.Forms.Control.FontChanged> | Occurs when the <xref:System.Windows.Forms.Control.Font> property value changes. |
|
||||
| <xref:System.Windows.Forms.Control.ForeColorChanged> | Occurs when the <xref:System.Windows.Forms.Control.ForeColor> property value changes. |
|
||||
| <xref:System.Windows.Forms.Control.LocationChanged> | Occurs when the <xref:System.Windows.Forms.Control.Location> property value has changed. |
|
||||
| <xref:System.Windows.Forms.Control.SizeChanged> | Occurs when the <xref:System.Windows.Forms.Control.Size> property value changes. |
|
||||
| <xref:System.Windows.Forms.Control.VisibleChanged> | Occurs when the <xref:System.Windows.Forms.Control.Visible> property value changes. |
|
||||
|
||||
For a full list of events, see the **Events** section of the [Control Class](xref:System.Windows.Forms.Control#events).
|
||||
|
||||
## 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 <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.
|
||||
|
||||
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](xref:System.Windows.Forms.Control#events).
|
||||
|
||||
## See also
|
||||
|
||||
- [How to handle an event](how-to-add-an-event-handler.md)
|
||||
- [Events overview](../forms/events.md)
|
||||
- [Using mouse events](../input-mouse/events.md)
|
||||
- [Using keyboard events](../input-keyboard/events.md)
|
||||
- <xref:System.Windows.Forms.Control?displayProperty=fullName>
|
||||
- <xref:System.Windows.Forms.Control.Click?displayProperty=fullName>
|
||||
- <xref:System.Windows.Forms.Button?displayProperty=fullName>
|
||||
@@ -19,15 +19,20 @@ Several Windows Forms controls can display images. These images can be icons tha
|
||||
|
||||
[!INCLUDE [desktop guide under construction](../../includes/desktop-guide-preview-note.md)]
|
||||
|
||||
## Designer
|
||||
## Display an image - designer
|
||||
|
||||
In the **Properties** window of Visual Studio, select the **Image** or **BackgroundImage** property of the control, and then select the ellipsis () to display the **Select Resource** dialog box and then select the image you want to display.
|
||||
In Visual Studio, use the Visual Designer to display an image.
|
||||
|
||||
:::image type="content" source="media/how-to-add-a-picture-to-a-control/properties-image.png" alt-text="Properties dialog with image property selected":::
|
||||
01. Open the Visual Designer of the form containing the control to change.
|
||||
01. Select the control.
|
||||
01. In the **Properties** pane, select the **Image** or **BackgroundImage** property of the control.
|
||||
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.
|
||||
|
||||
## Programmatic
|
||||
:::image type="content" source="media/how-to-add-a-picture-to-a-control/properties-image.png" alt-text="Properties dialog with image property selected":::
|
||||
|
||||
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.
|
||||
## Display an image - code
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
---
|
||||
title: How to add or remove an event handler
|
||||
description: Learn how to create an event handler at design-time with the Windows Forms Designer in Visual Studio or at run-time.
|
||||
ms.date: 07/16/2021
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "Windows Forms, event handling"
|
||||
- "event handlers [Windows Forms], creating"
|
||||
- "run time [Windows Forms], creating event handlers at"
|
||||
- "examples [Windows Forms], event handling"
|
||||
- "Button control [Windows Forms], event handlers"
|
||||
- "events [Windows Forms], connecting multiple to single event handler"
|
||||
- "event handlers [Windows Forms], connecting events to"
|
||||
- "menus [Windows Forms], event-handling methods for multiple menu items"
|
||||
- "Windows Forms controls, events"
|
||||
- "menu items [Windows Forms], multicasting event-handling methods"
|
||||
---
|
||||
|
||||
# How to handle an event (Windows Forms .NET)
|
||||
|
||||
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.
|
||||
|
||||
[!INCLUDE [desktop guide under construction](../../includes/desktop-guide-preview-note.md)]
|
||||
|
||||
## Handle an event - designer
|
||||
|
||||
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.
|
||||
|
||||
### Set the handler
|
||||
|
||||
Use the **Properties** pane to add or set the handler of an event:
|
||||
|
||||
01. Open the Visual Designer of the form containing the control to change.
|
||||
01. Select the control.
|
||||
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":::).
|
||||
01. Find the event you want to add a handler to, for example, the **Click** event:
|
||||
|
||||
:::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.":::
|
||||
|
||||
01. Do one of the following:
|
||||
|
||||
- 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.
|
||||
|
||||
- Use the selection box (:::image type="icon" source="../media/shared/visual-studio-chevron-button.png" border="false":::) to choose an existing handler.
|
||||
|
||||
The selection box will list all methods that have a compatible method signature for the event handler.
|
||||
|
||||
### Clear the handler
|
||||
|
||||
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:
|
||||
|
||||
01. Open the Visual Designer of the form containing the control to change.
|
||||
01. Select the control.
|
||||
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":::).
|
||||
01. Find the event containing the handler you want to remove, for example, the **Click** event:
|
||||
|
||||
:::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.":::
|
||||
|
||||
01. Right-click on the event and choose **Reset**.
|
||||
|
||||
## Handle an event - code
|
||||
|
||||
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.
|
||||
|
||||
### Add a handler
|
||||
|
||||
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.
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-add-an-event-handler/cs/Form1.cs" id="HandlerViaCode" highlight="12":::
|
||||
:::code language="vb" source="snippets/how-to-add-an-event-handler/vb/Form1.vb" id="HandlerViaCode" highlight="8":::
|
||||
|
||||
To run this code, do the following to a form with the Visual Studio Visual Designer:
|
||||
|
||||
01. Add a new button to the form and name it **Button1**.
|
||||
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":::).
|
||||
01. Double-click the **Click** event to generate a handler. This action opens the code window and generates a blank `Button1_Click` method.
|
||||
01. Replace the method code with the previous code above.
|
||||
|
||||
For more information about C# events, see [Events (C#)](/dotnet/csharp/programming-guide/events/)
|
||||
For more information about Visual Basic events, see [Events (Visual Basic)](/dotnet/visual-basic/programming-guide/language-features/events/)
|
||||
|
||||
### Remove the handler
|
||||
|
||||
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:
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-add-an-event-handler/cs/Form1.cs" id="RemoveHandler":::
|
||||
:::code language="vb" source="snippets/how-to-add-an-event-handler/vb/Form1.vb" id="RemoveHandler":::
|
||||
|
||||
This syntax can be used to remove any event handler from any event.
|
||||
|
||||
For more information about C# events, see [Events (C#)](/dotnet/csharp/programming-guide/events/)
|
||||
For more information about Visual Basic events, see [Events (Visual Basic)](/dotnet/visual-basic/programming-guide/language-features/events/)
|
||||
|
||||
## How to use multiple events with the same handler
|
||||
|
||||
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.
|
||||
|
||||
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/)
|
||||
|
||||
### Visual Basic
|
||||
|
||||
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/)
|
||||
|
||||
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:
|
||||
|
||||
:::code language="vb" source="snippets/how-to-add-an-event-handler/vb/Form2.vb" id="MultipleHandlers":::
|
||||
|
||||
## See also
|
||||
|
||||
- [Control events](events.md)
|
||||
- [Events overview](../forms/events.md)
|
||||
- [Using mouse events](../input-mouse/events.md)
|
||||
- [Using keyboard events](../input-keyboard/events.md)
|
||||
- <xref:System.Windows.Forms.Button?displayProperty=fullName>
|
||||
@@ -37,7 +37,7 @@ You can also set the text by using the [designer](#designer).
|
||||
|
||||
:::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.":::
|
||||
|
||||
01. In the **Properties** window, select the ellipsis button () next to the **Font** property.
|
||||
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.
|
||||
|
||||
:::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.":::
|
||||
|
||||
|
||||
BIN
Binary file not shown.
|
After Width: | Height: | Size: 7.2 KiB |
dotnet-desktop-guide/net/winforms/controls/snippets/how-to-add-an-event-handler/cs/Form1.Designer.cs
Generated
+91
@@ -0,0 +1,91 @@
|
||||
namespace cs
|
||||
{
|
||||
partial class Form1
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Windows Form Designer generated code
|
||||
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.button1 = new System.Windows.Forms.Button();
|
||||
this.label1 = new System.Windows.Forms.Label();
|
||||
this.textBox1 = new System.Windows.Forms.TextBox();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// button1
|
||||
//
|
||||
this.button1.Location = new System.Drawing.Point(107, 299);
|
||||
this.button1.Name = "button1";
|
||||
this.button1.Size = new System.Drawing.Size(75, 23);
|
||||
this.button1.TabIndex = 0;
|
||||
this.button1.Text = "button1";
|
||||
this.button1.UseVisualStyleBackColor = true;
|
||||
this.button1.Click += new System.EventHandler(this.button1_Click);
|
||||
this.button1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.button1_KeyDown);
|
||||
this.button1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.button1_KeyPress);
|
||||
this.button1.ChangeUICues += new System.Windows.Forms.UICuesEventHandler(this.button1_ChangeUICues);
|
||||
//
|
||||
// label1
|
||||
//
|
||||
this.label1.AutoSize = true;
|
||||
this.label1.Location = new System.Drawing.Point(329, 44);
|
||||
this.label1.Name = "label1";
|
||||
this.label1.Size = new System.Drawing.Size(38, 15);
|
||||
this.label1.TabIndex = 1;
|
||||
this.label1.Text = "label1";
|
||||
this.label1.DoubleClick += new System.EventHandler(this.label1_DoubleClick);
|
||||
this.label1.MouseEnter += new System.EventHandler(this.label1_MouseEnter);
|
||||
this.label1.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.label1_PreviewKeyDown);
|
||||
//
|
||||
// textBox1
|
||||
//
|
||||
this.textBox1.Location = new System.Drawing.Point(329, 85);
|
||||
this.textBox1.Name = "textBox1";
|
||||
this.textBox1.Size = new System.Drawing.Size(100, 23);
|
||||
this.textBox1.TabIndex = 2;
|
||||
//
|
||||
// Form1
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(800, 450);
|
||||
this.Controls.Add(this.textBox1);
|
||||
this.Controls.Add(this.label1);
|
||||
this.Controls.Add(this.button1);
|
||||
this.Name = "Form1";
|
||||
this.Text = "Form1";
|
||||
this.Load += new System.EventHandler(this.Form1_Load);
|
||||
this.ResumeLayout(false);
|
||||
this.PerformLayout();
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private System.Windows.Forms.Button button1;
|
||||
private System.Windows.Forms.Label label1;
|
||||
private System.Windows.Forms.TextBox textBox1;
|
||||
}
|
||||
}
|
||||
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace cs
|
||||
{
|
||||
public partial class Form1 : Form
|
||||
{
|
||||
public Form1()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void Form1_Load(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void label1_MouseEnter(object sender, EventArgs e)
|
||||
{
|
||||
label1.Text = "Mouse entered!";
|
||||
}
|
||||
|
||||
private void label1_DoubleClick(object sender, EventArgs e)
|
||||
{
|
||||
label1.Text = "Doubleclicked!";
|
||||
}
|
||||
|
||||
private void label1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
|
||||
{
|
||||
label1.Text = "preview key";
|
||||
|
||||
}
|
||||
|
||||
private void button1_KeyPress(object sender, KeyPressEventArgs e)
|
||||
{
|
||||
label1.Text = "button key";
|
||||
|
||||
}
|
||||
|
||||
private void a(object sender, KeyPressEventArgs e)
|
||||
{
|
||||
label1.Text = "button key";
|
||||
|
||||
}
|
||||
|
||||
private void button1_KeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void button1_ChangeUICues(object sender, UICuesEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//<HandlerViaCode>
|
||||
private void button1_Click(object sender, EventArgs e)
|
||||
{
|
||||
// Create and add the button
|
||||
Button myNewButton = new()
|
||||
{
|
||||
Location = new Point(10, 10),
|
||||
Size = new Size(120, 25),
|
||||
Text = "Do work"
|
||||
};
|
||||
|
||||
// Handle the Click event for the new button
|
||||
myNewButton.Click += MyNewButton_Click;
|
||||
this.Controls.Add(myNewButton);
|
||||
|
||||
// Remove this button handler so the user cannot do this twice
|
||||
//<RemoveHandler>
|
||||
button1.Click -= button1_Click;
|
||||
//</RemoveHandler>
|
||||
}
|
||||
|
||||
private void MyNewButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
//</HandlerViaCode>
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
<root>
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
</root>
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace cs
|
||||
{
|
||||
static class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// The main entry point for the application.
|
||||
/// </summary>
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
Application.SetHighDpiMode(HighDpiMode.SystemAware);
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
Application.Run(new Form1());
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net5.0-windows</TargetFramework>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
dotnet-desktop-guide/net/winforms/controls/snippets/how-to-add-an-event-handler/vb/Form1.Designer.vb
Generated
+62
@@ -0,0 +1,62 @@
|
||||
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
|
||||
Partial Class Form1
|
||||
Inherits System.Windows.Forms.Form
|
||||
|
||||
'Form overrides dispose to clean up the component list.
|
||||
<System.Diagnostics.DebuggerNonUserCode()>
|
||||
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
|
||||
Try
|
||||
If disposing AndAlso components IsNot Nothing Then
|
||||
components.Dispose()
|
||||
End If
|
||||
Finally
|
||||
MyBase.Dispose(disposing)
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
'Required by the Windows Form Designer
|
||||
Private components As System.ComponentModel.IContainer
|
||||
|
||||
'NOTE: The following procedure is required by the Windows Form Designer
|
||||
'It can be modified using the Windows Form Designer.
|
||||
'Do not modify it using the code editor.
|
||||
<System.Diagnostics.DebuggerStepThrough()>
|
||||
Private Sub InitializeComponent()
|
||||
Me.Button1 = New System.Windows.Forms.Button()
|
||||
Me.Button2 = New System.Windows.Forms.Button()
|
||||
Me.SuspendLayout()
|
||||
'
|
||||
'Button1
|
||||
'
|
||||
Me.Button1.Location = New System.Drawing.Point(354, 210)
|
||||
Me.Button1.Name = "Button1"
|
||||
Me.Button1.Size = New System.Drawing.Size(75, 23)
|
||||
Me.Button1.TabIndex = 0
|
||||
Me.Button1.Text = "Button1"
|
||||
Me.Button1.UseVisualStyleBackColor = True
|
||||
'
|
||||
'Button2
|
||||
'
|
||||
Me.Button2.Location = New System.Drawing.Point(354, 239)
|
||||
Me.Button2.Name = "Button2"
|
||||
Me.Button2.Size = New System.Drawing.Size(75, 23)
|
||||
Me.Button2.TabIndex = 1
|
||||
Me.Button2.Text = "Button2"
|
||||
Me.Button2.UseVisualStyleBackColor = True
|
||||
'
|
||||
'Form1
|
||||
'
|
||||
Me.AutoScaleDimensions = New System.Drawing.SizeF(7.0!, 15.0!)
|
||||
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
|
||||
Me.ClientSize = New System.Drawing.Size(800, 450)
|
||||
Me.Controls.Add(Me.Button2)
|
||||
Me.Controls.Add(Me.Button1)
|
||||
Me.Name = "Form1"
|
||||
Me.Text = "Form1"
|
||||
Me.ResumeLayout(False)
|
||||
|
||||
End Sub
|
||||
|
||||
Friend WithEvents Button1 As Windows.Forms.Button
|
||||
Friend WithEvents Button2 As Windows.Forms.Button
|
||||
End Class
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
<root>
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
</root>
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
Imports System.Windows.Forms
|
||||
Imports System.Drawing
|
||||
|
||||
Partial Public Class Form1
|
||||
|
||||
Public Sub New()
|
||||
InitializeComponent()
|
||||
End Sub
|
||||
|
||||
Private Sub Button1_Click_2(sender As Object, e As EventArgs)
|
||||
'<CreateControl>
|
||||
Dim label1 As New Label With {.Text = "&First Name",
|
||||
.Location = New Point(10, 10),
|
||||
.TabIndex = 10}
|
||||
|
||||
Dim field1 As New TextBox With {.Location = New Point(label1.Location.X,
|
||||
label1.Bounds.Bottom + Padding.Top),
|
||||
.TabIndex = 11}
|
||||
|
||||
Controls.Add(label1)
|
||||
Controls.Add(field1)
|
||||
'</CreateControl>
|
||||
End Sub
|
||||
|
||||
'<HandlerViaCode>
|
||||
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
|
||||
'Create and add the button
|
||||
Dim myNewButton As New Button() With {.Location = New Point(10, 10),
|
||||
.Size = New Size(120, 25),
|
||||
.Text = "Do work"}
|
||||
|
||||
'Handle the Click event for the new button
|
||||
AddHandler myNewButton.Click, AddressOf MyNewButton_Click
|
||||
Me.Controls.Add(myNewButton)
|
||||
|
||||
'Remove this button handler so the user cannot do this twice
|
||||
'<RemoveHandler>
|
||||
RemoveHandler Button1.Click, AddressOf Button1_Click
|
||||
'</RemoveHandler>
|
||||
End Sub
|
||||
|
||||
Private Sub MyNewButton_Click(sender As Object, e As EventArgs)
|
||||
|
||||
End Sub
|
||||
'</HandlerViaCode>
|
||||
End Class
|
||||
dotnet-desktop-guide/net/winforms/controls/snippets/how-to-add-an-event-handler/vb/Form2.Designer.vb
Generated
+62
@@ -0,0 +1,62 @@
|
||||
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
|
||||
Partial Class Form2
|
||||
Inherits System.Windows.Forms.Form
|
||||
|
||||
'Form overrides dispose to clean up the component list.
|
||||
<System.Diagnostics.DebuggerNonUserCode()> _
|
||||
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
|
||||
Try
|
||||
If disposing AndAlso components IsNot Nothing Then
|
||||
components.Dispose()
|
||||
End If
|
||||
Finally
|
||||
MyBase.Dispose(disposing)
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
'Required by the Windows Form Designer
|
||||
Private components As System.ComponentModel.IContainer
|
||||
|
||||
'NOTE: The following procedure is required by the Windows Form Designer
|
||||
'It can be modified using the Windows Form Designer.
|
||||
'Do not modify it using the code editor.
|
||||
<System.Diagnostics.DebuggerStepThrough()> _
|
||||
Private Sub InitializeComponent()
|
||||
Me.Button1 = New System.Windows.Forms.Button()
|
||||
Me.Button2 = New System.Windows.Forms.Button()
|
||||
Me.SuspendLayout()
|
||||
'
|
||||
'Button1
|
||||
'
|
||||
Me.Button1.Location = New System.Drawing.Point(74, 123)
|
||||
Me.Button1.Name = "Button1"
|
||||
Me.Button1.Size = New System.Drawing.Size(75, 23)
|
||||
Me.Button1.TabIndex = 0
|
||||
Me.Button1.Text = "Button1"
|
||||
Me.Button1.UseVisualStyleBackColor = True
|
||||
'
|
||||
'Button2
|
||||
'
|
||||
Me.Button2.Location = New System.Drawing.Point(74, 152)
|
||||
Me.Button2.Name = "Button2"
|
||||
Me.Button2.Size = New System.Drawing.Size(75, 23)
|
||||
Me.Button2.TabIndex = 1
|
||||
Me.Button2.Text = "Button2"
|
||||
Me.Button2.UseVisualStyleBackColor = True
|
||||
'
|
||||
'Form2
|
||||
'
|
||||
Me.AutoScaleDimensions = New System.Drawing.SizeF(7.0!, 15.0!)
|
||||
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
|
||||
Me.ClientSize = New System.Drawing.Size(800, 450)
|
||||
Me.Controls.Add(Me.Button2)
|
||||
Me.Controls.Add(Me.Button1)
|
||||
Me.Name = "Form2"
|
||||
Me.Text = "Form2"
|
||||
Me.ResumeLayout(False)
|
||||
|
||||
End Sub
|
||||
|
||||
Friend WithEvents Button1 As Windows.Forms.Button
|
||||
Friend WithEvents Button2 As Windows.Forms.Button
|
||||
End Class
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
<root>
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
</root>
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
Public Class Form2
|
||||
'<MultipleHandlers>
|
||||
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
|
||||
'Do some work to handle the events
|
||||
End Sub
|
||||
'</MultipleHandlers>
|
||||
End Class
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
Imports System
|
||||
Imports System.Collections.Generic
|
||||
Imports System.Linq
|
||||
Imports System.Threading.Tasks
|
||||
Imports System.Windows.Forms
|
||||
|
||||
Module Program
|
||||
Sub Main(args As String())
|
||||
Application.SetHighDpiMode(HighDpiMode.SystemAware)
|
||||
Application.EnableVisualStyles()
|
||||
Application.SetCompatibleTextRenderingDefault(False)
|
||||
Application.Run(New Form1())
|
||||
End Sub
|
||||
|
||||
End Module
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net5.0-windows</TargetFramework>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
<StartupObject>project.Program</StartupObject>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 224 B |
|
Before Width: | Height: | Size: 211 B After Width: | Height: | Size: 211 B |
Binary file not shown.
|
After Width: | Height: | Size: 263 B |
@@ -35,6 +35,8 @@ items:
|
||||
href: controls/layout.md
|
||||
- name: Labels
|
||||
href: controls/labels.md
|
||||
- name: Events
|
||||
href: controls/events.md
|
||||
- name: Custom controls
|
||||
href: controls/custom.md
|
||||
- name: Custom painting and drawing
|
||||
@@ -55,6 +57,8 @@ items:
|
||||
href: controls/how-to-dock-and-anchor.md
|
||||
- name: Set the image displayed by a control
|
||||
href: controls/how-to-add-a-picture-to-a-control.md
|
||||
- name: Add or remove event handlers
|
||||
href: controls/how-to-add-an-event-handler.md
|
||||
- name: User input - keyboard
|
||||
items:
|
||||
- name: Overview
|
||||
|
||||
Reference in New Issue
Block a user