c# textbox.focus not - how to put focus on TextBox when the form load?





7 Answers

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

working set and

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




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.




Textbox.Focus() "Tries" to set focus on the textbox element. In case of the element visibility is hidden for example, Focus() will not work. So make sure that your element is visible before calling Focus().




use form shown event and set

MyTextBox.Focus();



it's worked for me set tabindex to 0 this.yourtextbox.TabIndex = 0;




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.




In jquery set focus

$(function() {
  $("#txtBox1").focus();
});

or in Javascript you can do

window.onload = function() {
  document.getElementById("txtBox1").focus();
};



Related


Tags

c#   winforms