textbox.Focus() not working in C#

56,175

Solution 1

Use Select() instead:

recipientEmail_tbx.Select();

Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx

Solution 2

Add Delay some miliSec. Delay then call Focus() and Not forget to put inside Dispatcher.

Task.Delay(100).ContinueWith(_ =>
     {
         Application.Current.Dispatcher.Invoke(new Action(() =>
         {
             TextBoxNAme.Focus();
         }));
     });

Solution 3

Even I tried with lots of above solutions but none of them worked for me as am trying to focus on page load. Finally I got this solution and it worked.

private void txtBox_LayoutUpdated(object sender, EventArgs e)
{
    txtBox.Focus();
}
Share:
56,175

Related videos on Youtube

Dark Knight
Author by

Dark Knight

Updated on July 06, 2020

Comments

  • Dark Knight
    Dark Knight almost 4 years

    am wondering why this code fails to focus the textbox...?

    private void sendEmail_btn_Click(object sender, EventArgs e)
    {    
        String sendTo = recipientEmail_tbx.Text.Trim();
        if (!IsValidEmailAddress(sendTo))
        {
            MessageBox.Show("Please Enter valid Email address","Cognex" MessageBoxButtons.OK, MessageBoxIcon.Error);                
            recipientEmail_tbx.Focus();
        }
    }
    
    • Cody Gray
      Cody Gray over 13 years
      The code you posted doesn't compile—the best overload match for the MessageBox.Show call has some invalid arguments. Once that's fixed (I specified a caption), the code works exactly as expected and sets the focus to the textbox. What's the problem? Do you want the text that has already been entered to appear selected?
    • Dark Knight
      Dark Knight over 13 years
      @Cody:its edited...check now:)
    • Cody Gray
      Cody Gray over 13 years
      Like I said, even once I fixed the call to MessageBox.Show by adding a caption string (exactly the same thing you did), the code still works fine. After the message box is displayed, the focus is set back to the textbox. Why do you say it's "not working"? Were you hoping for the text that has been entered in the textbox to appear selected/highlighted?
    • Dark Knight
      Dark Knight over 13 years
      no no...if cursor stops in text box thats enough...but its not working here..even I tried this.ActiveControl=recipientEmail_tbx...no luck :(
    • Daniel Peñalba
      Daniel Peñalba over 13 years
      @Sisya: Did you tried Select() instead?
    • Dark Knight
      Dark Knight over 13 years
      @Daniel:yup...even its not working for me..horrible!!
    • Daniel Peñalba
      Daniel Peñalba over 13 years
      @Sisya: mmm it seems to be other issue. Maybe when you are focusing the control, the form is not selected, or blocked by a background operation?
    • Dark Knight
      Dark Knight over 13 years
      how can form is not selected ?send button is in the same form
    • Cody Gray
      Cody Gray over 13 years
      I've tried several different things (like a default button on the form) to see if I can't throw a wrench into the operation, but I still can't repro the situation you describe. Can you give us any more information about your setup?
    • Alexandre Leites
      Alexandre Leites over 13 years
      Have you tried checking your recipientEmail_tbx's TabStop if it is set to true
    • Dark Knight
      Dark Knight over 13 years
      @all:thanks for ur time...I fixed it by using this.Focus..:) MessageBox pop up was the culprit here..
  • Admin
    Admin over 10 years
    Error 1 No overload for method 'Select' takes 0 arguments
  • Alfie
    Alfie about 10 years
    The parameterless overload has existed since 1.1: msdn.microsoft.com/en-us/library/…
  • hfrmobile
    hfrmobile over 7 years
    When Select() does not work for you: Often some other control takes the focus. So ensure that your call to Select() to your control is the last one.
  • Fabien Teulieres
    Fabien Teulieres about 4 years
    While I don't like these hacky "add-a-delay-to-make-things-work" type of solutions, it did get me out of a bind on my multi-threaded C# app. So thanks and +1 for you good sir!
  • Kushan Gowda
    Kushan Gowda over 3 years
    Use Select() instead!
  • WiiLF
    WiiLF over 2 years
    Works great, thank you man