how to put focus on TextBox when the form load?

320,853

Solution 1

Set theActiveControl property of the form and you should be fine.

this.ActiveControl = yourtextboxname;

Solution 2

check your tab order and make sure the textbox is set to zero

Solution 3

You cannot set focus to a control if it has not been rendered. Form.Load() occurs before the controls are rendered.

Go to the form's events and double click the "Shown" event. In the form's shown event handler call the control.Focus() method.

    private void myForm_Shown(object sender, EventArgs e)
    {
        // Call textbox's focus method
        txtMyTextbox.Focus();
    }

Solution 4

You could try:

MyTextBox.Select();

According to the documentation:

The Select method activates the control if the control's Selectable style bit is set to true in ControlStyles, it is contained in another control, and all its parent controls are both visible and enabled.

You can first check if the control can be selectable by inspecting the MyTextBox.CanSelect property.

Solution 5

If you only want to set the focus the first time the form is shown, try handling the Form.Shown event and doing it there. Otherwise use Control.VisibleChanged.

Share:
320,853

Related videos on Youtube

Gali
Author by

Gali

Updated on February 25, 2021

Comments

  • Gali
    Gali about 3 years

    I have in my C# program textBox

    I need that when the program start, the focus will be on the textBox

    I try this on Form_Load:

    MyTextBox.Focus();
    

    but it wont work

  • Alex Jolig
    Alex Jolig about 9 years
    I tried this for ComboBox. But it doesn't work either!
  • David Carrigan
    David Carrigan over 8 years
    Thanks this worked for me where everything else did not. Not sure why Tab Index = 0 won't work but there are probably strange order of operations going on while loading the form/showing dialog.
  • b_in_U
    b_in_U over 8 years
    active control shows to me. but it doesn't work functionally. :(
  • Polamin Singhasuwich
    Polamin Singhasuwich over 7 years
    TRY THIS >>> this.ActiveControl = yourtextboxname.Control;
  • Ben Voigt
    Ben Voigt over 7 years
    This might be a good answer for a web site question, but this one is tagged c# and winforms.
  • Levon
    Levon over 7 years
    This works great, but why didn't MyTextBox.Focus(); work - that seems to work fine once the program is running.
  • Bitterblue
    Bitterblue almost 6 years
    Write this.ActiveControl = textBox1; Everyone understands the meaning of "textBox1". "youttextboxname" sounds like ... = "MyTextBox";
  • apincik
    apincik almost 5 years
    Working. Or just tabindex to be the first cursor "focusable" element in the form.
  • Tushar R.
    Tushar R. over 4 years
    If we call textBox1.select(); in form load event handler, it sets the focus in the textBox1.
  • Todd Hoatson
    Todd Hoatson almost 2 years
    I had TabIndex = 0 for the first textBox on my dialog, but it didn't work. I found out that this was due to the fact that the first 3 textBoxes we placed on a groupBox. So the TabIndex values for those textBoxes within the groupBox were not even being considered by the dialog, and the first control visible to the dialog was an enabled control outside the groupBox with the lowest TabIndex value. So watch out for that...
  • Todd Hoatson
    Todd Hoatson almost 2 years
    This worked perfectly for me. I placed this line directly after the call to InitializeComponent() in the Form constructor. This was needed because my textBox was not placed directly on the Form, but inside a groupBox on the Form. Therefore, the TabIndex of my textBox was not visible to the Form. (Maybe the same thing happens if the textBox is in a table layout...?)