Merge pull request #1117 from dotnet/publish-13111

This commit is contained in:
Bill Wagner
2021-07-20 06:13:10 -04:00
committed by GitHub
32 changed files with 941 additions and 145 deletions
+12
View File
@@ -423,6 +423,18 @@
{
"source_path": "dotnet-desktop-guide/net/winforms/controls/how-to-dock-controls-on-windows-forms.md",
"redirect_url": "/dotnet/desktop/winforms/controls/how-to-dock-and-anchor?view=netdesktop-5.0"
},
{
"source_path": "dotnet-desktop-guide/net/winforms/how-to-create-event-handlers-at-run-time-for-windows-forms.md",
"redirect_url": "/dotnet/desktop/winforms/controls/how-to-add-an-event-handler?view=netdesktop-5.0"
},
{
"source_path": "dotnet-desktop-guide/framework/winforms/controls/how-to-add-an-event-handler.md",
"redirect_url": "/dotnet/desktop/winforms/how-to-create-event-handlers-at-run-time-for-windows-forms?view=netframeworkdesktop-4.8"
},
{
"source_path": "dotnet-desktop-guide/net/winforms/how-to-connect-multiple-events-to-a-single-event-handler-in-windows-forms.md",
"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"
}
]
}
@@ -1,7 +1,7 @@
---
title: "Popup Overview"
description: Learn about the Windows Presentation Foundation Popup control, which provides a way to display content in a window that floats over the current application.
ms.date: "03/30/2017"
ms.date: "07/14/2021"
helpviewer_keywords:
- "controls [WPF], Popup"
- "Popup control [WPF], about Popup control"
@@ -19,12 +19,11 @@ The <xref:System.Windows.Controls.Primitives.Popup> control provides a way to di
<a name="APopupExample"></a>
## Creating a Popup
The following example shows how to define a <xref:System.Windows.Controls.Primitives.Popup> control that is the child element of a <xref:System.Windows.Controls.Button> control. Because a <xref:System.Windows.Controls.Button> can have only one child element, this example places the text for the <xref:System.Windows.Controls.Button> and the <xref:System.Windows.Controls.Primitives.Popup> controls in a <xref:System.Windows.Controls.StackPanel>. The content of the <xref:System.Windows.Controls.Primitives.Popup> appears in a <xref:System.Windows.Controls.TextBlock> control, which displays its text in a separate window that floats over the application window near the related <xref:System.Windows.Controls.Button> control.
[!code-xaml[PopupSimple#1](~/samples/snippets/csharp/VS_Snippets_Wpf/PopupSimple/CSharp/Window1.xaml#1)]
[!code-xaml[PopupSimple#CreatePopupCodeXAML](~/samples/snippets/csharp/VS_Snippets_Wpf/PopupSimple/CSharp/Window1.xaml#createpopupcodexaml)]
The following example shows how to define a <xref:System.Windows.Controls.Primitives.Popup> control that is the child element of a <xref:System.Windows.Controls.Primitives.ToggleButton> control. Because a `ToggleButton` can have only one child element, this example places the text for the `ToggleButton` and the `Popup` controls in a <xref:System.Windows.Controls.StackPanel>. The content of the `Popup` is displayed in a separate window that floats over the application window near the related `ToggleButton` control.
:::code language="xaml" source="snippets/popup-overview/csharp/Window1.xaml" id="ToggleButtonCodeless":::
<a name="PopupUses"></a>
## Controls That Implement a Popup
You can build <xref:System.Windows.Controls.Primitives.Popup> controls into other controls. The following controls implement the <xref:System.Windows.Controls.Primitives.Popup> control for specific uses:
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWpf>true</UseWpf>
</PropertyGroup>
</Project>
@@ -0,0 +1,64 @@
<Window x:Class="Popup_Properties_Sample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Popup_Properties_Sample"
>
<StackPanel>
<Border HorizontalAlignment="Left" BorderThickness="2" Margin="10,10,0,0"
BorderBrush="Green" Background="Beige" Width="300">
<TextBlock Foreground="Blue" FontSize="12" Margin="10,10,10,10"
TextWrapping="Wrap" >
This sample shows examples of a Popup controls
that are the logical children of a Buttons. Each Popup
window is positioned with respect to a Button. However,
because the Popup content is contained in its own window,
the Popup is not a visual child of the Button.
</TextBlock>
</Border>
<TextBlock/>
<TextBlock Foreground="Blue" FontSize="12" Margin="20,10,10,10"
TextWrapping="Wrap" Width="300" HorizontalAlignment="Left">
Type the content you want to appear in the Popup in the text
box below and then click the button to display the Popup
</TextBlock>
<TextBox Name="myTextBox" Margin="20,0,0,0" Foreground="HotPink"
Width="150" HorizontalAlignment="Left" TextChanged="setColors">
Type your Popup text here
</TextBox>
<!--<ToggleButtonCodeless>-->
<ToggleButton x:Name="TogglePopupButton" Height="30" Width="150" HorizontalAlignment="Left">
<StackPanel>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center">
<Run Text="Is button toggled? " />
<Run Text="{Binding IsChecked, ElementName=TogglePopupButton}" />
</TextBlock>
<Popup Name="myPopup" IsOpen="{Binding IsChecked, ElementName=TogglePopupButton}">
<Border BorderThickness="1">
<TextBlock Name="myPopupText" Background="LightBlue" Foreground="Blue" Padding="30">
Popup Text
</TextBlock>
</Border>
</Popup>
</StackPanel>
</ToggleButton>
<!--</ToggleButtonCodeless>-->
<TextBlock Foreground="Blue" FontSize="12" Margin="10,40,10,0"
TextWrapping="Wrap">
Click the button to create a Popup by using code
</TextBlock>
<!--<SnippetCreatePopupCodeXAML>-->
<Button HorizontalAlignment="Left" Click="CreatePopup"
Width="150" Margin="20,10,0,0">
<StackPanel Name="ButtonContentContainer">
<TextBlock>Create Popup</TextBlock>
</StackPanel>
</Button>
<!--</SnippetCreatePopupCodeXAML>-->
</StackPanel>
</Window>
@@ -36,20 +36,20 @@ namespace Popup_Properties_Sample
}
//</SnippetIsFocused>
//<SnippetCreatePopupCode>
//<CreatePopup>
private void CreatePopup(object sender, RoutedEventArgs e)
{
//<SnippetCreatePopup>
Popup codePopup = new Popup();
TextBlock popupText = new TextBlock();
popupText.Text = "Popup Text";
popupText.Background = Brushes.LightBlue;
popupText.Foreground = Brushes.Blue;
TextBlock popupText = new()
{
Text = "Popup Text",
Background = Brushes.LightBlue,
Foreground = Brushes.Blue
};
codePopup.Child = popupText;
//</SnippetCreatePopup>
aStackPanel.Children.Add(codePopup);
ButtonContentContainer.Children.Add(codePopup);
codePopup.IsOpen = true;
}
//</SnippetCreatePopupCode>
//</CreatePopup>
}
}
}
@@ -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 (![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.
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 (![Ellipsis button (...) in the Properties window of Visual Studio](../media/visual-studio-ellipsis-button.png)) 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.":::
@@ -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;
}
}
@@ -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>
}
}
@@ -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>
@@ -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());
}
}
}
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
</Project>
@@ -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
@@ -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>
@@ -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
@@ -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
@@ -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>
@@ -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
@@ -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
@@ -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

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
@@ -1,58 +0,0 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D3D69C55-A09F-473C-99B8-876826A68796}</ProjectGuid>
<RootNamespace>Popup_Properties_Sample</RootNamespace>
<AssemblyName>Popup Properties Sample</AssemblyName>
<WarningLevel>4</WarningLevel>
<OutputType>winexe</OutputType>
<ApplicationVersion>1.0.0.*</ApplicationVersion>
<!-- Most people will use Publish dialog in Visual Studio to increment this -->
<ProductVersion>10.0.20821</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>.\bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>false</DebugSymbols>
<Optimize>true</Optimize>
<OutputPath>.\bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="UIAutomationProvider" />
<Reference Include="UIAutomationTypes" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Runtime.Serialization" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="app.xaml" />
<Page Include="Window1.xaml" />
<Compile Include="app.xaml.cs">
<DependentUpon>app.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="Window1.xaml.cs">
<DependentUpon>Window1.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
@@ -1,64 +0,0 @@
<Window x:Class="Popup_Properties_Sample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Popup_Properties_Sample"
>
<StackPanel>
<Border HorizontalAlignment="Left" BorderThickness="2" Margin="10,10,0,0"
BorderBrush="Green" Background="Beige" Width="300">
<TextBlock Foreground="Blue" FontSize="12" Margin="10,10,10,10"
TextWrapping="Wrap" >
This sample shows examples of a Popup controls
that are the logical children of a Buttons. Each Popup
window is positioned with respect to a Button. However,
because the Popup content is contained in its own window,
the Popup is not a visual child of the Button.
</TextBlock>
</Border>
<TextBlock/>
<TextBlock Foreground="Blue" FontSize="12" Margin="20,10,10,10"
TextWrapping="Wrap" Width="300" HorizontalAlignment="Left">
Type the content you want to appear in the Popup in the text
box below and then click the button to display the Popup
</TextBlock>
<TextBox Name="myTextBox" Margin="20,0,0,0" Foreground="HotPink"
Width="150" HorizontalAlignment="Left" TextChanged="setColors">
Type your Popup text here
</TextBox>
<!--<Snippet1>-->
<Button HorizontalAlignment="Left" Click="DisplayPopup"
Width="150" Margin="20,10,0,0">
<StackPanel>
<TextBlock>Display Your Popup Text</TextBlock>
<!--<Snippet2>-->
<Popup Name="myPopup">
<TextBlock Name="myPopupText"
Background="LightBlue"
Foreground="Blue">
Popup Text
</TextBlock>
</Popup>
<!--</Snippet2>-->
</StackPanel>
</Button>
<!--</Snippet1>-->
<TextBlock Foreground="Blue" FontSize="12" Margin="10,40,10,0"
TextWrapping="Wrap">
Click the button to create a Popup by using code
</TextBlock>
<!--<SnippetCreatePopupCodeXAML>-->
<Button Name="ButtonForPopup" HorizontalAlignment="Left"
Click="CreatePopup"
Width="150" Margin="20,10,0,0">
<StackPanel Name="aStackPanel">
<TextBlock>Create Popup</TextBlock>
</StackPanel>
</Button>
<!--</SnippetCreatePopupCodeXAML>-->
</StackPanel>
</Window>
@@ -1 +0,0 @@
wcsamp_components_popupsimple