I have a TextBox in my C# program. I need focus to be on this TextBox when the program starts.
I tried this on Form_Load:
MyTextBox.Focus();
but it doesn't work.
How do I put focus on this when the form loads?
I have a TextBox in my C# program. I need focus to be on this TextBox when the program starts.
I tried this on Form_Load:
MyTextBox.Focus();
but it doesn't work.
How do I put focus on this when the form loads?
I solved my problem with changing "TabIndex" property of TextBox. I set 0 for TextBox that I want to focus it on Form when the program start.
Set Tab Index property's value = 0 and then in form load function write :
YourTextboxName.Focus();
It will work.
You can use either textBox1.select(); or the TabIndex in textbox setting. TabIndex=0 focoused first.
Finally i found the problem i was using metro framework and all your solutions will not work with metroTextBox, and all your solutions will work with normal textBox in load , show , visibility_change ,events, even the tab index = 0 is valid.
// private void Form1_VisibleChanged(object sender, EventArgs e)
// private void Form1__Shown(object sender, EventArgs e)
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Select();
this.ActiveControl=textBox1;
textBox1.Focus();
}
Set Tabstop to True and TabIndex to the minimum to the control on which you need focus.
e.g. If you have 2 TextBoxes : TextBox1 and TextBox2, set Tabstop to True for both and TabIndex to 0 and 1 respectively. When the form loads, the focus will be on TextBox1 and on the press of 'Tab' key, the focus will move to TextBox2.
on your form, go to properties and make sure that "TopMost" property is set to true, that will solve your problem.