#838 – Using a Label’s Access Key to Give Focus to Another Control

An access key is a character that the user can press, in combination with the Alt key, in order to activate or give a control focus.

Controls that the user can interact with and that have their own label typically can define their own access keys.  (E.g. A Button).  Controls that don’t have their own label can be activated or receive focus by using the access key of a nearby Label.

To pair a Label’s access key with another control, you:

  • Define the access key using an underscore in the Label’s Content property
  • Associate the related control using the Label’s Target property
    <StackPanel Orientation="Horizontal">
        <Label Content="Your _Name" VerticalAlignment="Center"
               Target="{Binding ElementName=txtName}"/>
        <TextBox Name="txtName" VerticalAlignment="Center"
                 Width="120"/>
    </StackPanel>

When the user presses Alt+N, the TextBox gains focus (and the user can then begin typing to enter their name).

838-001

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

One Response to #838 – Using a Label’s Access Key to Give Focus to Another Control

  1. Pingback: Dew Drop – June 10, 2013 (#1,564) | Alvin Ashcraft's Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: