i have a textbox with tab index set to 0, i want to keep the tab index the same but i dont want it to got focus when the windows form loads. how to do that?

Dani AI

Generated

As pointed out, forcing focus/selection during load is a common quick fix and, as confirmed, selecting the caret position solved the immediate problem. A bit more context and a couple more robust options are useful when that simple call misbehaves (it sometimes does when WinForms finishes layout/activation after Load).

WinForms chooses the initial active control by TabIndex among focusable controls, and focus changes you make in Form.Load can be overwritten by the framework. Two reliable patterns to avoid the textbox being focused on startup while keeping its TabIndex are:

  • Set the initial active control after the form is shown (handle Form.Shown or override OnShown) so the change happens after activation.
  • Defer the focus change using BeginInvoke from Load so it runs after the current layout/activation work finishes.

Example patterns:

private void Form1_Shown(object sender, EventArgs e)
{
    this.ActiveControl = someOtherControl; // choose a control that can receive focus
}
private void Form1_Load(object sender, EventArgs e)
{
    this.BeginInvoke((Action)(() => someOtherControl.Focus()));
}

Notes and cautions: the target control must be focusable (a Label or disabled control won’t work). If the goal is to skip the textbox in tab order, consider toggling its TabStop, but that changes runtime tab behavior; if you only want to avoid the initial focus, prefer Shown/BeginInvoke. For official behavior and APIs see the Microsoft docs on Control.Focus, ContainerControl.ActiveControl, Form.Shown and Control.BeginInvoke.

Does textbox1.Focus(); not work in the form_load method?

Oooppss its textbox1.Select(); in the Form_Load method.

commented: good job +5

let me try, if it works, i will kiss you on the forehead.

it worked :)

smartAppendOnlyTextBox1.Select(smartAppendOnlyTextBox1.Text.Length,0);

thank you so much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.