how to put focus on TextBox when the form load?
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:
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.
Related videos on Youtube
Gali
Updated on February 25, 2021Comments
-
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 about 9 yearsI tried this for
ComboBox
. But it doesn't work either! -
David Carrigan over 8 yearsThanks 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 over 8 yearsactive control shows to me. but it doesn't work functionally. :(
-
Polamin Singhasuwich over 7 yearsTRY THIS >>> this.ActiveControl = yourtextboxname.Control;
-
Ben Voigt over 7 yearsThis might be a good answer for a web site question, but this one is tagged
c#
andwinforms
. -
Levon over 7 yearsThis works great, but why didn't
MyTextBox.Focus();
work - that seems to work fine once the program is running. -
Bitterblue almost 6 yearsWrite
this.ActiveControl = textBox1;
Everyone understands the meaning of "textBox1". "youttextboxname" sounds like... = "MyTextBox";
-
apincik almost 5 yearsWorking. Or just tabindex to be the first cursor "focusable" element in the form.
-
Tushar R. over 4 yearsIf we call textBox1.select(); in form load event handler, it sets the focus in the textBox1.
-
Todd Hoatson almost 2 yearsI 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 almost 2 yearsThis 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...?)