Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the generic soution for arbitrary amount of TextBox'es.</p> <p>The initialization of the TextBox'es list:</p> <pre><code>private readonly List&lt;TextBox&gt; _textBoxes; public MainWindow() { InitializeComponent(); _textBoxes = new List&lt;TextBox&gt; { _textBox1, _textBox2, _textBox3 }; } </code></pre> <p>The version with KeyUp event:</p> <pre><code>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; } </code></pre> <p>The above version dissalows to jump through TextBox'es in press and hold scenario. To get around this, use TextChanged event:</p> <pre><code>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; } </code></pre> <p>Third solution with PreviewKeyDown that supports only Key.Delete:</p> <pre><code>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; } </code></pre> <p>Fourth solution also with PreviewKeyDown that supports both Key.Delete and Key.Back:</p> <pre><code>private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key != Key.Delete &amp;&amp; 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload