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

  • 질문

  • 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();
            }
    • 이동됨 OmegaMan 2009년 7월 16일 목요일 오후 4:35 (From:Visual C# Language)
    2009년 7월 16일 목요일 오전 8:59

답변

  • 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.
    • 답변으로 표시됨 Aland Li 2009년 7월 22일 수요일 오전 1:47
    2009년 7월 16일 목요일 오후 9:24
  •     protected override void OnShown(EventArgs e)
        {
            textBox1.Focus();
            base.OnShown(e);
        }
    
    • 답변으로 표시됨 Aland Li 2009년 7월 22일 수요일 오전 1:47
    2009년 7월 16일 목요일 오후 2: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!
    • 답변으로 표시됨 Aland Li 2009년 7월 22일 수요일 오전 1:47
    2009년 7월 16일 목요일 오후 3: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

    • 답변으로 표시됨 Aland Li 2009년 7월 22일 수요일 오전 1:47
    2009년 7월 17일 금요일 오전 8:29

모든 응답

  • 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.
    • 답변으로 제안됨 Razib007 2013년 6월 3일 월요일 오전 1:45
    • 답변으로 제안 취소됨 Razib007 2013년 6월 3일 월요일 오전 1:45
    2009년 7월 16일 목요일 오전 9: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
    • 답변으로 제안됨 ELBates4 2014년 4월 4일 금요일 오후 3:27
    2009년 7월 16일 목요일 오전 9: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.
    2009년 7월 16일 목요일 오전 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 ~
    2009년 7월 16일 목요일 오전 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.
    2009년 7월 16일 목요일 오전 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 :-)

    2009년 7월 16일 목요일 오전 10:37
  •     protected override void OnShown(EventArgs e)
        {
            textBox1.Focus();
            base.OnShown(e);
        }
    
    • 답변으로 표시됨 Aland Li 2009년 7월 22일 수요일 오전 1:47
    2009년 7월 16일 목요일 오후 2: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!
    • 답변으로 표시됨 Aland Li 2009년 7월 22일 수요일 오전 1:47
    2009년 7월 16일 목요일 오후 3: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();

    2009년 7월 16일 목요일 오후 3: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.
    • 답변으로 표시됨 Aland Li 2009년 7월 22일 수요일 오전 1:47
    2009년 7월 16일 목요일 오후 9: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

    • 답변으로 표시됨 Aland Li 2009년 7월 22일 수요일 오전 1:47
    2009년 7월 17일 금요일 오전 8:29
  • hello,
    Like Tergiver said, You should try in Form_Shown event . it will definatly work.
    2009년 7월 17일 금요일 오전 8: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
    2009년 7월 17일 금요일 오전 8:44
  • MSDN says,

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

    Shown: Occurs whenever the form is first displayed.
    2009년 7월 17일 금요일 오전 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.
    2009년 7월 17일 금요일 오후 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!

    • 답변으로 제안됨 Razib007 2013년 6월 3일 월요일 오전 1:46
    2013년 6월 2일 일요일 오전 8:44

  • Thank you for the help

    :)

    2013년 12월 19일 목요일 오전 5: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();

    2020년 5월 2일 토요일 오후 7:48