Search code examples
c#textboxfocus

Focus on the first empty textBox in C#


I am trying to focus on the first empty TextBox item.

How can I focus on the first empty TextBox?


Solution

  • Do a foreach loop to iterate through all TextBoxes in your Form.
    Example:

    foreach (TextBox tb in Controls.OfType<TextBox>()) {
        if (string.IsNullOrWhiteSpace(tb.Text)) {
            tb.Focus();
            break;
        }
    }