locked
C# - Set Focus on TextBox - Which Event? RRS feed

  • Question

  • I have a Form and a Textbox control, once the Form loads up, and I want to set the cursor FOCUS on the TextBox, however, I cannot do this and I have no reason why, please help~~

            private void Form_Load(object sender, EventArgs e)
            {
                this.TextBox1.Focus();
            }
    • Moved by OmegaMan Thursday, July 16, 2009 4:35 PM (From:Visual C# Language)
    Thursday, July 16, 2009 8:59 AM

Answers

  • The Focus() method can only work if the control window is displayed.  It isn't yet when the Load event runs.  The workaround is simple:

      textBox1.Select();

    Setting the TabIndex property value properly is another way.  And using Shown.

    Hans Passant.
    • Marked as answer by Aland Li Wednesday, July 22, 2009 1:47 AM
    Thursday, July 16, 2009 9:24 PM
  •     protected override void OnShown(EventArgs e)
        {
            textBox1.Focus();
            base.OnShown(e);
        }
    
    • Marked as answer by Aland Li Wednesday, July 22, 2009 1:47 AM
    Thursday, July 16, 2009 2:58 PM
  • You could set the tab order of the TextBox to 0 and ensure that the tab order of the other controls are a number higher than 0. The form should then come up with the TextBox with focus and you should not need any code.

    Also, check this out:

    Open a form in the designer.

    Select View | TabOrder

    This feature allows you to easily set the order that the user will access the controls on the form.

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by Aland Li Wednesday, July 22, 2009 1:47 AM
    Thursday, July 16, 2009 3:15 PM
  • I finally made it work !!!

    Here below is the solution:
    -----------------------------------
    Put the following "PAIR of code" in the FORM_LOAD event, do NOT put the following code after InitializeComponent();

     

    this.WindowState = FormWindowState.Maximized;

     

    this.TextBox1.Focus();

    I don't know why, but this is the only way made it work

    • Marked as answer by Aland Li Wednesday, July 22, 2009 1:47 AM
    Friday, July 17, 2009 8:29 AM

All replies

  • Are you sure that Form_Load is even being called? Have you tried placing this.TextBox1.Focus() right after InitializeComponent()?
    Regards,
    Kristaps.

    P.S.: Epic shoop is epic.
    • Proposed as answer by Razib007 Monday, June 3, 2013 1:45 AM
    • Unproposed as answer by Razib007 Monday, June 3, 2013 1:45 AM
    Thursday, July 16, 2009 9:23 AM
  • Yes, I did.

    I have tried a number of "ways" but I still can't do the FOCUS.

    P.S. I can do this in VB.NET
    • Proposed as answer by ELBates4 Friday, April 4, 2014 3:27 PM
    Thursday, July 16, 2009 9:24 AM
  • Try calling TextBox1.Focus() [without this.] right after InitializeComponent(). I just tried and it worked for me. If it doesn't double-check all the code, names etc.

    Regards,
    Kristaps.

    P.S.: Epic shoop is epic.
    Thursday, July 16, 2009 10:03 AM
  • No, it doesn't work at my side.

    It seems like there is not much coding I can "investigate", I just put my code right after InitializeComponent(), not working

    then, I put it in Form_Load event, not working either.

    Since there is only one single line of code, and there is no "appearing" error I can trace.  Very Strange to me....

    Pls help ~
    Thursday, July 16, 2009 10:14 AM
  • Is the textbox's "focusable" checkbox set to true?

    Maybe try starting a new project with just one textbox and a button, which focuses on the textbox?
    Regards,
    Kristaps.

    P.S.: Epic shoop is epic.
    Thursday, July 16, 2009 10:30 AM
  • Yes, some Form works, but some are not, 

    I did set FOCUS right after InitializeComponent();

    #1 Login Form  <--- SET FOCUS - it works !
    #2 MDI parent 
    #3 MDI child    <--- SET FOCUS - it doesn't

    About #3, I also put the FOCUS right after  InitializeComponent();, how come it doesn't work?  Anything I should do in order to "DEBUG" it?

    Pls help :-)

    Thursday, July 16, 2009 10:37 AM
  •     protected override void OnShown(EventArgs e)
        {
            textBox1.Focus();
            base.OnShown(e);
        }
    
    • Marked as answer by Aland Li Wednesday, July 22, 2009 1:47 AM
    Thursday, July 16, 2009 2:58 PM
  • You could set the tab order of the TextBox to 0 and ensure that the tab order of the other controls are a number higher than 0. The form should then come up with the TextBox with focus and you should not need any code.

    Also, check this out:

    Open a form in the designer.

    Select View | TabOrder

    This feature allows you to easily set the order that the user will access the controls on the form.

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by Aland Li Wednesday, July 22, 2009 1:47 AM
    Thursday, July 16, 2009 3:15 PM
  • To my understanding, can I think the logic as like this

    Step 1: InitializeComponent();
    Step 2: Textbox1.SetFocus();

    I mean, can I force the logic to run .SetFocus() even if the TabOrder has been set differently.

    Of course, I can do this without any problem.  But I simply got my problem in the following "flow" (i.e. #3)

    #1 Login Form  <--- SET FOCUS - it works !
    #2 MDI parent 
    #3 MDI child    <--- SET FOCUS - it doesn't

    A while ago, I found some problem in a function under #2 MDI parent (I used F8 to do Debug), surprisely, I found .SetFocus() was not occured in the order I expect.

    Anyway, I just want to say, the following "flow" might be "affected" sometimes, I just found this fact, but don't know how to explain further

    Step 1: InitializeComponent();
    Step 2: Textbox1.SetFocus();

    Thursday, July 16, 2009 3:42 PM
  • The Focus() method can only work if the control window is displayed.  It isn't yet when the Load event runs.  The workaround is simple:

      textBox1.Select();

    Setting the TabIndex property value properly is another way.  And using Shown.

    Hans Passant.
    • Marked as answer by Aland Li Wednesday, July 22, 2009 1:47 AM
    Thursday, July 16, 2009 9:24 PM
  • I finally made it work !!!

    Here below is the solution:
    -----------------------------------
    Put the following "PAIR of code" in the FORM_LOAD event, do NOT put the following code after InitializeComponent();

     

    this.WindowState = FormWindowState.Maximized;

     

    this.TextBox1.Focus();

    I don't know why, but this is the only way made it work

    • Marked as answer by Aland Li Wednesday, July 22, 2009 1:47 AM
    Friday, July 17, 2009 8:29 AM
  • hello,
    Like Tergiver said, You should try in Form_Shown event . it will definatly work.
    Friday, July 17, 2009 8:39 AM
  • Thanks you guys,

    But now, what is the different between Form_Load and Form_Shown?

    Do you mean Form_Shown is after Form_Load event?

    Please advice
    Friday, July 17, 2009 8:44 AM
  • MSDN says,

    Load: Occurs before a form is displayed for the first time.

    Shown: Occurs whenever the form is first displayed.
    Friday, July 17, 2009 10:06 AM
  • Hi iHandler,

     

    We need to know which control gets focused in the Activated event handler by check the ActiveControl property of the form. You can set a break point in the activated handler or use MesssageBox.Show(this.ActiveControl.Name) to know that. Then handle the GotFocus event of the activated control and focus the control which you want to focus. Assume the activated control is textBox2 and you want to focus textBox1. Then you can write code as follow:

    private void Form1_Load(object sender, EventArgs e)

    {

        textBox2.GotFocus += new EventHandler(ctrl_GotFocus);

    }

    void ctrl_GotFocus(object sender, EventArgs e)

    {

        textBox1.Focus();

        (sender as Control).GotFocus -= new EventHandler(ctrl_GotFocus);

    }

     

    Let me know if this helps.
    Aland Li

     


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Friday, July 17, 2009 12:55 PM
  • Dear iHandler,

    I also got the same problem. The best and simplest solution that I got is:

    textBox1.DeselectAll();  //Here, your textbox's name is "textBox1"

    So, your program code should be:

            private void Form_Load(object sender, EventArgs e)
            {
                this.TextBox1.Focus();

                textBox1.DeselectAll();

            }

    Enjoy friend!

    • Proposed as answer by Razib007 Monday, June 3, 2013 1:46 AM
    Sunday, June 2, 2013 8:44 AM

  • Thank you for the help

    :)

    Thursday, December 19, 2013 5:39 AM
  • Since this is the load event of the form, the form itself needs to be brought into focus before the text box. Thus, another line need to be inserted here, so the working code will look like:

    this.Select();
    textbox1.Select();

    Saturday, May 2, 2020 7:48 PM