How do you auto highlight text in a textbox control when the control gains focus.
In Windows Forms and WPF:
textbox.SelectionStart = 0; textbox.SelectionLength = textbox.Text.Length;
If you want to do it for your whole WPF application you can do the following: - In the file App.xaml.cs
protected override void OnStartup(StartupEventArgs e) { //works for tab into textbox EventManager.RegisterClassHandler(typeof(TextBox), TextBox.GotFocusEvent, new RoutedEventHandler(TextBox_GotFocus)); //works for click textbox EventManager.RegisterClassHandler(typeof(Window), Window.GotMouseCaptureEvent, new RoutedEventHandler(Window_MouseCapture)); base.OnStartup(e); } private void TextBox_GotFocus(object sender, RoutedEventArgs e) { (sender as TextBox).SelectAll(); } private void Window_MouseCapture(object sender, RoutedEventArgs e) { var textBox = e.OriginalSource as TextBox; if (textBox != null) textBox.SelectAll(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With