This C# program demonstrates how to set a key in a label to give a textbox focus in a WPF program. Putting the underscore before "F" in the label, allows the focus to be given to the textbox when the "F" key is pressed. Once "F" is pressed, text can be entered into the textbox, as shown below with the text "XoaX.net".
<Window x:Class="Label1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Label Target="{Binding ElementName=tb}">_File</Label> <TextBox Name="tb" Height="30" Width="100" Margin="5,24,407,265" Background="lightgray"/> <TextBlock Name="qTextBlock" HorizontalAlignment="Left" Margin="5,60,0,0" TextWrapping="Wrap" VerticalAlignment="Top"> Press "f" to set the focus on the gray textbox. Then text can be entered into it. </TextBlock> </Grid> </Window>
using System.Windows; namespace Label1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // This example sets the focus to the given text box when the "f" key is pressed. // The preceding underscore sets the access key to "f". } } }
© 20072025 XoaX.net LLC. All rights reserved.