#304 – Defining an Access Key That Gives Focus to a Different Control

For controls that don’t have their own textual caption, it’s common to define an access key for a nearby label.  When that access key is activated, the control without the caption gets the focus.

In the example below, there is a TextBox where you can enter your name and the nearby Label “Enter Name”.  This screenshot was taken after the ALT key was pressed, so the ‘N’ in “Enter Name” is underlined, indicating that ‘N’ is the access key.  If the user presses ALT-N, the TextBox will get focus and they can then begin typing the name.

You can specify the control that should be the target of an access key by using the Target property on the control where you define the access key.

            <!-- Access key on Label gives focus to TextBox -->
            <Label Content="Enter _Name:" VerticalAlignment="Center" Target="txtName" />
            <TextBox Name="txtName" Width="150" Margin="10"/>

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 #304 – Defining an Access Key That Gives Focus to a Different Control

  1. Stephen says:

    It would be nice if there was a way to use ctrl instead of alt….

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: