Make a "hotkey" to focus a TextBox in WPF
Asked Answered
R

2

14

I am trying to make a hot key (ie Ctrl + Y) that will change the focus to a text box.

I am a transplant from Delphi, and this is confusing me. In Delphi 5 this was so, so easy. (On the caption of the label you could just add an & before the letter you want to make the hot key. After you point the label at the TextBox the hotkey would work.)

For WPF, I am seeing horrific examples in WPF involving calling out to Win32 calls or making a command for each hotkey (and other such heavy implementations).

I find it hard to believe that an IDE and Languange version that was new in 1999 (Delphi 5) has a better system (than WPF) for something as simple as Hotkeys.

Surely I am missing something. If you know, please tell me what it is.

Rebuke answered 4/5, 2012 at 16:13 Comment(3)
may be this thread is helpful to you https://mcmap.net/q/158969/-keyboard-shortcuts-in-wpfTowboat
@Habib.OSU - It looks like you can use an _ instead of an &. But how do you connect the label to the TextBox? (And I tried putting an underscore in my label and it just printed the _). (The rest of the question seems to discuss using command for hot keys. I would like to avoid that if I can (I will have a lot of controls and wiring them all up with commands seems like overkill.))Rebuke
@Habib.OSU - I found the answer I was looking for. If you are interested you can see it posted below.Rebuke
R
27

So, I should have kept looking harder. I got it shortly after asking this question.

They way you do this is that you create a label and set its Content (something like this Content="_Years Of Service")

You then bind the Label's Target to a text box. (Target="{Binding ElementName=SomeTextBox}")

Now if you press Alt + Y it will move the focus to SomeTextBox.

Here is the full binding:

<Label Content="_Label" Target="{Binding ElementName=SomeTextBox}" />
<TextBox Name="SomeTextBox" />
Rebuke answered 4/5, 2012 at 16:31 Comment(1)
Except that if you are assigning the string from the code, using SomeLabel.Content = "_Label";, the underline will be displayed as-is. The correct code is something like SomeLabel.Content = new AccessText { Text = "_Label" };Outstare
P
0

Method-1:

Place underscore _ in front of the letter that you want to be the Hotkey in XAML.

Method-2:

Using code-behind you have to use the AccessText control as it is responsible for displaying an underscore under the mnemonic character (indicating an access key or hotkey) and recognising the hotkey.

Note: Sometime underscore under HotKey are not visible until you press the ALT key

Output: enter image description here

MainWindow.xaml

<Window x:Class="EmptyWPFApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="450" Width="800">
    <Grid >
        <StackPanel>
            <Label Content="_Area" Target="{Binding ElementName=T1}"/>
            <TextBox Name="T1">Press ALT+A to get focus</TextBox>
            <Label Content="A_bout" Target="{Binding ElementName=T2}"/>
            <TextBox Name="T2">Press ALT+B to get focus</TextBox>
            <Label x:Name="Label3" Target="{Binding ElementName=T3}"/>
            <TextBox x:Name="T3">Press ALT+C to get focus</TextBox>
        </StackPanel>
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Windows;
using System.Windows.Controls;
namespace EmptyWPFApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Label3.Content = new AccessText { Text = "Atta_ck" };
        }
    }
}
Phyfe answered 4/3 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.