Files
docs-desktop/dotnet-desktop-guide/net/winforms/controls/how-to-create-access-keys.md
T
Andy De GeorgeandGenevieve Warren 184e6c9a0c Initial publish of WinForms for .NET 5. (#96)
* 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]>
2020-10-28 10:44:16 -07:00

92 lines
3.8 KiB
Markdown

---
title: Create Access Keys for Controls
description: Learn how to set the access key shortcut on a control or label in Windows Forms for .NET.
ms.date: 10/26/2020
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "controls [Windows Forms], access keys"
- "Button control [Windows Forms], access keys"
- "dialog box controls [Windows Forms], mnemonics"
- "access keys [Windows Forms], creating for controls"
- "mnemonics"
- "ampersand character in shortcut key"
- "Windows Forms controls, access keys"
- "examples [Windows Forms], controls"
- "Text property [Windows Forms], specifying access keys for controls"
- "keyboard shortcuts [Windows Forms], creating for controls"
- "access keys [Windows Forms], Windows Forms"
- "ALT key"
---
# 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 <kbd>Alt</kbd> 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 <kbd>Alt</kbd>.
Controls that cannot receive focus can't have access keys, except label controls.
[!INCLUDE [desktop guide under construction](../../includes/desktop-guide-preview-note.md)]
## 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.
```vb
' Set the letter "P" as an access key.
Button1.Text = "&Print"
```
```csharp
// 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](xref:System.Windows.Forms.Label.Text) property, the next control in the sequential tab order is focused.
Using the example from the [Programmatic](#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.
```vb
' Set the letter "P" as an access key.
Label1.Text = "&Print"
Label1.TabIndex = 9
Button1.TabIndex = 10
```
```csharp
// 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`:
```vb
' Set the letter "P" as an access key.
Button1.Text = "Print && Close"
```
```csharp
// 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":::
## See also
- [How to: Set the text displayed by a Windows Forms control](how-to-set-the-display-text.md)
- <xref:System.Windows.Forms.Button>
- <xref:System.Windows.Forms.Label>