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

  • Domanda

  • 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();
            }
    • Spostato OmegaMan giovedì 16 luglio 2009 16:35 (From:Visual C# Language)
    giovedì 16 luglio 2009 08:59

Risposte

  • 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.
    • Contrassegnato come risposta Aland Li mercoledì 22 luglio 2009 01:47
    giovedì 16 luglio 2009 21:24
  •     protected override void OnShown(EventArgs e)
        {
            textBox1.Focus();
            base.OnShown(e);
        }
    
    • Contrassegnato come risposta Aland Li mercoledì 22 luglio 2009 01:47
    giovedì 16 luglio 2009 14:58
  • 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!
    • Contrassegnato come risposta Aland Li mercoledì 22 luglio 2009 01:47
    giovedì 16 luglio 2009 15:15
  • 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

    • Contrassegnato come risposta Aland Li mercoledì 22 luglio 2009 01:47
    venerdì 17 luglio 2009 08:29

Tutte le risposte

  • 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.
    • Proposto come risposta Razib007 lunedì 3 giugno 2013 01:45
    • Proposta come risposta annullata Razib007 lunedì 3 giugno 2013 01:45
    giovedì 16 luglio 2009 09:23
  • 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
    • Proposto come risposta ELBates4 venerdì 4 aprile 2014 15:27
    giovedì 16 luglio 2009 09:24
  • 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.
    giovedì 16 luglio 2009 10:03
  • 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 ~
    giovedì 16 luglio 2009 10:14
  • 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.
    giovedì 16 luglio 2009 10:30
  • 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 :-)

    giovedì 16 luglio 2009 10:37
  •     protected override void OnShown(EventArgs e)
        {
            textBox1.Focus();
            base.OnShown(e);
        }
    
    • Contrassegnato come risposta Aland Li mercoledì 22 luglio 2009 01:47
    giovedì 16 luglio 2009 14:58
  • 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!
    • Contrassegnato come risposta Aland Li mercoledì 22 luglio 2009 01:47
    giovedì 16 luglio 2009 15:15
  • 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();

    giovedì 16 luglio 2009 15:42
  • 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.
    • Contrassegnato come risposta Aland Li mercoledì 22 luglio 2009 01:47
    giovedì 16 luglio 2009 21:24
  • 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

    • Contrassegnato come risposta Aland Li mercoledì 22 luglio 2009 01:47
    venerdì 17 luglio 2009 08:29
  • hello,
    Like Tergiver said, You should try in Form_Shown event . it will definatly work.
    venerdì 17 luglio 2009 08:39
  • 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
    venerdì 17 luglio 2009 08:44
  • MSDN says,

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

    Shown: Occurs whenever the form is first displayed.
    venerdì 17 luglio 2009 10:06
  • 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.
    venerdì 17 luglio 2009 12:55
  • 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!

    • Proposto come risposta Razib007 lunedì 3 giugno 2013 01:46
    domenica 2 giugno 2013 08:44

  • Thank you for the help

    :)

    giovedì 19 dicembre 2013 05:39
  • 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();

    sabato 2 maggio 2020 19:48