C # WPF Focus previous textBox if current erased

I have a project where I need to focus the previous field if the current one is empty, but the user continues to delete. For example, when you type CD-Key. You have a pair of blocks of 4-5 characters. And if you delete the third text block, for example, you will be forced to return to the second text block immediately after the third becomes emprty.

if (textBox2.Text.Length == 0) { Keyboard.Focus(textBox1); } 

This code works fine, but given that I have another onfocus event, so textBox2 becomes empty as soon as it receives focus, and because of the code above the focus returning back to textBox1. So he is fixated.

If I get it right, I need to catch a click on the Delete button, right? But here is my problem. I do not know how to insert this code

 private void Window_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Delete) { if (textBox2.Text.Length == 0) { Keyboard.Focus(textBox1); } } } 

inside this function:

 private void textBox2_TextChanged(object sender, TextChangedEventArgs e) { if (textBox2.Text.Length == 2) { Keyboard.Focus(textBox3); } // HERE I NEED SOMETHING LIKE ELSE IF (e.Key == Key.Delete) {... } 

Help me please. UPD I tried another solution, but it does not work:

 private void textBox2_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Delete) { if (textBox2.Text.Length == 0) { Keyboard.Focus(textBox1); } } } 
+4
source share
3 answers

Here is a common suffix for an arbitrary number of TextBoxes.

Initializing the TextBox'es list:

 private readonly List<TextBox> _textBoxes; public MainWindow() { InitializeComponent(); _textBoxes = new List<TextBox> { _textBox1, _textBox2, _textBox3 }; } 

Version with KeyUp event:

 private void TextBox_KeyUp(object sender, KeyEventArgs e) { if (e.Key == Key.Tab) return; var current = (TextBox)sender; if (current.Text.Any()) return; var index = _textBoxes.IndexOf(current); if (index == 0) return; var previous = _textBoxes[index - 1]; previous.Focus(); previous.CaretIndex = previous.Text.Length; } 

The above version does not allow you to go through TextBoxes in the click and hold scripts. To get around this, use the TextChanged event:

 private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { var current = (TextBox)sender; if (current.Text.Any()) return; var index = _textBoxes.IndexOf(current); if (index == 0) return; var previous = _textBoxes[index - 1]; previous.Focus(); previous.CaretIndex = previous.Text.Length; } 

The third solution is with PreviewKeyDown, which only supports Key.Delete:

 private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key != Key.Delete) return; var current = (TextBox)sender; if (current.Text.Length != 0) return; var index = _textBoxes.IndexOf(current); if (index == 0) return; var previous = _textBoxes[index - 1]; previous.Focus(); previous.CaretIndex = 0; } 

The fourth solution is also with PreviewKeyDown, which supports both Key.Delete and Key.Back:

 private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key != Key.Delete && e.Key != Key.Back) return; var current = (TextBox)sender; if (current.Text.Length != 0) return; var index = _textBoxes.IndexOf(current); if (index == 0) return; var previous = _textBoxes[index - 1]; previous.Focus(); if (e.Key == Key.Delete) previous.CaretIndex = 0; } 
+1
source

Finally. It works:

 private void textBox2_KeyUp(object sender, KeyEventArgs e) { if (e.Key == Key.Delete) { if (textBox2.Text.Length == 0) { Keyboard.Focus(textBox1); } } } 
0
source

This is not tested, but should also work.

 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void textBox2_KeyDown(object sender, KeyEventArgs e) { if (textBox2.Text == "") { textBox1.Focus(); } } private void textBox3_KeyDown(object sender, KeyEventArgs e) { if (textBox3.Text == "") { textBox2.Focus(); } } } 
0
source

Source: https://habr.com/ru/post/1495127/


All Articles