#840 – Access Keys Should Be Unique within A Window

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.  Because a user’s key press is associated with a particular control, you should ensure that each access key is unique within the parent window.

If you do happen to define the same access key for multiple controls within a window, pressing the access key will toggle the focus between the controls, rather than activating them.  In the example below, press Alt+T will toggle the focus back and forth between the two buttons.  If you then change the second access key to something different, pressing Alt+T will activate (press) the first button.

        <Button Content="_This" VerticalAlignment="Top"
                Padding="10,5" Margin="5"
                Click="DoThis_Click"/>
        <Button Content="_That" VerticalAlignment="Top"
                Padding="10,5" Margin="5"
                Click="DoThat_Click"/>
Advertisement

#839 – A Label’s Target Could Have Its Own Label

You typically set the Target of a Label to a control that doesn’t have its own label.  The purpose is that when the user presses the Label’s access key, the target control will receive focus.

There’s nothing preventing you, however, from specifying the Target as a control that would normally have its own caption.

For example, you normally set a Button’s access key to one of the characters in its Content property–i.e. the text that appears on the face of the button.  In the example below, however, we use a Label to specify an access key for the Button, since the button’s content is just an asterisk character.  When you press the Label’s access key, the Button will activate.

    <StackPanel Orientation="Horizontal">
        <Label Content="_Calculate" VerticalAlignment="Center"
               Target="{Binding ElementName=btnCalc}"/>
        <Button Name="btnCalc" Content="*" VerticalAlignment="Center"
                Padding="10,5" Click="btnCalc_Click"/>
    </StackPanel>

839-001
839-002

#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