how to put focus on TextBox when the form load?

C#WinformsFocusSetfocus

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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

this.ActiveControl = yourtextboxname;

Solution 2 - C#

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

Solution 3 - C#

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 - C#

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 - C#

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.

Solution 6 - C#

The reason you can't get it to work is because the Load event is called before the form is drawn or rendered.

It like telling a pizza place how to make your pizza, and then asking them to send you a picture of how much pepperoni is on your pizza before they made it.

using System;
using System.Windows.Forms;

namespace Testing
{
    public partial class TestForm : Form
    {
        public TestForm()
        {
            InitializeComponent();

            Load += TestForm_Load;

            VisibleChanged += TestForm_VisibleChanged;

            Shown += TestForm_Shown;

            Show();

        }

        private void TestForm_Load(object sender, EventArgs e)
        {
            MessageBox.Show("This event is called before the form is rendered.");
        }

        private void TestForm_VisibleChanged(object sender, EventArgs e)
        {
            MessageBox.Show("This event is called before the form is rendered.");
        }

        private void TestForm_Shown(object sender, EventArgs e)
        {
            MessageBox.Show("This event is called after the form is rendered.");
            txtFirstName.Focus();
        }
    }
}

Solution 7 - C#

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().

Solution 8 - C#

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.

Solution 9 - C#

use form shown event and set

MyTextBox.Focus();

Solution 10 - C#

Set Tab Index property's value = 0 and then in form load function write :

YourTextboxName.Focus();

It will work.

Solution 11 - C#

You can use either textBox1.select(); or the TabIndex in textbox setting. TabIndex=0 focoused first.

Solution 12 - C#

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();
    }

Solution 13 - C#

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.

Solution 14 - C#

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

Solution 15 - C#

on your form, go to properties and make sure that "TopMost" property is set to true, that will solve your problem.

Solution 16 - C#

In jquery set focus

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

or in Javascript you can do

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGaliView Question on Stackoverflow
Solution 1 - C#V4VendettaView Answer on Stackoverflow
Solution 2 - C#SpacemancraigView Answer on Stackoverflow
Solution 3 - C#DavidGView Answer on Stackoverflow
Solution 4 - C#Neil KnightView Answer on Stackoverflow
Solution 5 - C#Andy JohnsonView Answer on Stackoverflow
Solution 6 - C#BenView Answer on Stackoverflow
Solution 7 - C#GRCView Answer on Stackoverflow
Solution 8 - C#Mehmet Taha MeralView Answer on Stackoverflow
Solution 9 - C#Nitin...View Answer on Stackoverflow
Solution 10 - C#Shakaib AkhtarView Answer on Stackoverflow
Solution 11 - C#user9248597View Answer on Stackoverflow
Solution 12 - C#MarkLeeView Answer on Stackoverflow
Solution 13 - C#Aparna RatheeshView Answer on Stackoverflow
Solution 14 - C#Muhafil SaiyedView Answer on Stackoverflow
Solution 15 - C#MustafaView Answer on Stackoverflow
Solution 16 - C#Sudip BhattacharyaView Answer on Stackoverflow