How do I put focus on a TextBox when a form loads?
Asked Answered
R

17

188

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?

Rodd answered 6/7, 2011 at 13:29 Comment(0)
S
402

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

this.ActiveControl = yourtextboxname;
Selvage answered 6/7, 2011 at 13:36 Comment(8)
I tried this for ComboBox. But it doesn't work either!Avaunt
Thanks 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.Apologist
active control shows to me. but it doesn't work functionally. :(Amin
TRY THIS >>> this.ActiveControl = yourtextboxname.Control;Amal
This works great, but why didn't MyTextBox.Focus(); work - that seems to work fine once the program is running.Mill
Write this.ActiveControl = textBox1; Everyone understands the meaning of "textBox1". "youttextboxname" sounds like ... = "MyTextBox";Irfan
This 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...?)Jezabelle
@Mill it will, but you have to make sure the form is rendered prior to calling it. Call it in the shown event methodBloodfin
B
30

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();
    }
Bloodfin answered 31/3, 2017 at 3:2 Comment(0)
B
25

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

Bailly answered 6/7, 2011 at 13:32 Comment(1)
Working. Or just tabindex to be the first cursor "focusable" element in the form.Amasa
M
15

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.

Mcbryde answered 6/7, 2011 at 13:35 Comment(0)
S
13

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.

Smail answered 6/7, 2011 at 13:36 Comment(0)
T
8

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();
        }
    }
}
Traduce answered 20/8, 2016 at 14:8 Comment(0)
B
7

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

Bethink answered 15/3, 2013 at 19:56 Comment(0)
L
5

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.

Lilywhite answered 4/10, 2016 at 19:33 Comment(0)
M
4

use form shown event and set

MyTextBox.Focus();
Morgue answered 9/10, 2015 at 17:41 Comment(1)
Not work when a form loads.Druggist
P
4

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

YourTextboxName.Focus();

It will work.

Proviso answered 11/9, 2018 at 6:57 Comment(0)
M
4

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();
    }
Mandeville answered 6/6, 2020 at 11:30 Comment(0)
A
2

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

Acreage answered 21/1, 2018 at 20:3 Comment(2)
If we call textBox1.select(); in form load event handler, it sets the focus in the textBox1.Rhines
I 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...Jezabelle
S
1

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.

Sudatory answered 18/6, 2018 at 2:44 Comment(0)
A
0

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

Abbatial answered 1/12, 2016 at 6:47 Comment(0)
N
0

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

Nevil answered 4/9, 2019 at 7:56 Comment(0)
P
0

This is one simple way to put the textbox into focus when loading a C# form. It's similar to the above, but with a bit less coding. I can't recall where I first found the solution, but I've used it for a few years, and credits go to them.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.ActiveControl = txt_PrtrIP;
        txt_PrtrIP.Focus();
    }
Poler answered 8/4 at 21:34 Comment(0)
B
-5

In jquery set focus

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

or in Javascript you can do

window.onload = function() {
  document.getElementById("txtBox1").focus();
};
Boong answered 1/12, 2016 at 10:30 Comment(1)
This might be a good answer for a web site question, but this one is tagged c# and winforms.Jacobus

© 2022 - 2024 — McMap. All rights reserved.