3

How to set cursor focus to a TextBox?

I have a window pop up with a TextBox and would like to focus the cursor to it. So a user can directly type text.

I could not find a proper property. Is there one?

John Threepwood
  • 15,593
  • 27
  • 93
  • 149
  • possible duplicate of http://stackoverflow.com/questions/1345391/set-focus-on-textbox-in-wpf – BRBT Mar 18 '14 at 16:53

2 Answers2

7

To set focus on a textbox when your form loads you can do this:

private void Form_Load(object sender, EventArgs e)
    {
        SomeTextBox.Select();
    }

Note** You have to put it within the Form_Load event.

BRBT
  • 1,467
  • 8
  • 28
  • 48
2

In WPF, try this:

FocusManager.SetFocusedElement(parentElement, txtMyTextBox)

Read more about FocusManager.SetFocusedElement here.

OR

txtMyTextBox.Focusable = true;
Keyboard.Focus(txtMyTextBox);
Raging Bull
  • 18,593
  • 13
  • 50
  • 55