* Migrate inprogress winforms contnet from docs * Fix preview note * Fix vb code * Minor fixes to keyboard articles * Minor adjustment to overview * Test redirects for 4.0 -> 5.0 * adjust links * Add mouse input section winforms (#81) * Update projects to .NET 5 * Update keyboard desc * Finish mouse events * Add remaining mouse articles;code * finish mouse input * minor fixes * Remove branch restriction (#82) * Update build-validation.yml (#84) * Update build-validation.yml (#85) * Fix vb proj * Add WinForms tutorial (#91) * redirect between overview * New create an app tutorial * Fix markdown * Fix preserve view setting * Add forms articles (#93) * Add forms articles * Fix warnings * Winforms publish (#95) * Prep TOC for publish * Updated date * Automatic how-to topic type * Update desc/headers * Fix links * Fix build errors * Add preview note * Update toc landing page * Convert old style project files * update download links * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> * Adjust number key * Update dotnet-desktop-guide/net/winforms/overview/index.md * Try toc position task via bookmark * Improve images * Remove sentence * File redirects Co-authored-by: Genevieve Warren <[email protected]>
3.8 KiB
title, description, ms.date, dev_langs, helpviewer_keywords
| title | description | ms.date | dev_langs | helpviewer_keywords | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Create Access Keys for Controls | Learn how to set the access key shortcut on a control or label in Windows Forms for .NET. | 10/26/2020 |
|
|
Add an access key shortcut to a control (Windows Forms .NET)
An access key is an underlined character in the text of a menu, menu item, or the label of a control such as a button. With an access key, the user can "click" a button by pressing the Alt key in combination with the predefined access key. For example, if a button runs a procedure to print a form, and therefore its Text property is set to "Print," adding an ampersand (&) before the letter "P" causes the letter "P" to be underlined in the button text at run time. The user can run the command associated with the button by pressing Alt.
Controls that cannot receive focus can't have access keys, except label controls.
[!INCLUDE desktop guide under construction]
Designer
In the Properties window of Visual Studio, set the Text property to a string that includes an ampersand (&) before the letter that will be the access key. For example, to set the letter "P" as the access key, enter &Print.
:::image type="content" source="media/how-to-create-access-keys/properties-text.png" alt-text="Properties dialog with text property selected and access key":::
Programmatic
Set the Text property to a string that includes an ampersand (&) before the letter that will be the shortcut.
' Set the letter "P" as an access key.
Button1.Text = "&Print"
// Set the letter "P" as an access key.
button1.Text = "&Print";
Use a label to focus a control
Even though a label cannot be focused, it has the ability to focus the next control in the tab order of the form. Each control is assigned a value to the xref:System.Windows.Forms.Control.TabIndex property, generally in ascending sequential order. When the access key is assigned to the Label.Text property, the next control in the sequential tab order is focused.
Using the example from the Programmatic section, if the button didn't have any text set, but instead presented an image of a printer, you could use a label to focus the button.
' Set the letter "P" as an access key.
Label1.Text = "&Print"
Label1.TabIndex = 9
Button1.TabIndex = 10
// Set the letter "P" as an access key.
label1.Text = "&Print";
label1.TabIndex = 9
button1.TabIndex = 10
Display an ampersand
When setting the text or caption of a control that interprets an ampersand (&) as an access key, use two consecutive ampersands (&&) to display a single ampersand. For example, the text of a button set to "Print && Close" displays in the caption of Print & Close:
' Set the letter "P" as an access key.
Button1.Text = "Print && Close"
// Set the letter "P" as an access key.
button1.Text = "Print && Close";
:::image type="content" source="media/how-to-create-access-keys/double-ampersand.png" alt-text="displaying an ampersand in a button":::