How to remove the focus from a TextBox in WinForms?

203,447

Solution 1

You need some other focusable control to move the focus to.

Note that you can set the Focus to a Label. You might want to consider where you want the [Tab] key to take it next.

Also note that you cannot set it to the Form. Container controls like Form and Panel will pass the Focus on to their first child control. Which could be the TextBox you wanted it to move away from.

Solution 2

Focusing on the label didn't work for me, doing something like label1.Focus() right? the textbox still has focus when loading the form, however trying Velociraptors answer, worked for me, setting the Form's Active control to the label like this:

private void Form1_Load(object sender, EventArgs e)  
{ 
    this.ActiveControl = label1;       
}

Solution 3

You can add the following code:

this.ActiveControl = null;  //this = form

Solution 4

You can also set the forms activecontrol property to null like

ActiveControl = null;

Solution 5

Try disabling and enabling the textbox.

Share:
203,447
Callum Rogers
Author by

Callum Rogers

I am interested in Functional Programming, Compilers, Programming Languages, Interpreters and Reactive Programming. Github

Updated on February 10, 2022

Comments

  • Callum Rogers
    Callum Rogers about 2 years

    I need to remove the focus from several TextBoxes. I tried using:

    textBox1.Focused = false;
    

    Its ReadOnly property value is true.

    I then tried setting the focus on the form, so as to remove it from all the TextBoxes, but this also fails to work:

    this.Focus();
    

    and the function returns false when a textbox is selected.

    So, how do I remove the focus from a TextBox?

  • Callum Rogers
    Callum Rogers almost 15 years
    Thanks; I just tried focusing on a label and now the textbox becomes unfocused. It seems you cannot focus on a form for some reason.
  • Henk Holterman
    Henk Holterman almost 15 years
    Container Controls (Form, Panel) will pass the Focus on to their first child control.
  • Nick
    Nick almost 14 years
    This works pretty slick as it automatically selects the next control in the tab list in the meantime.
  • Albert Oldfield
    Albert Oldfield almost 14 years
    I am developing in Silverlight using MVVM and implemented this using a behavior targeting a TextBox. Since I didn't have another UIElement handy to set focus to the Disable/Enable solution worked wonders. Thanks!
  • eladyanai
    eladyanai about 11 years
    i wish i could give you million arrow up's. i tried EVERYTHING else that people suggested, this is the only one that worked. for some reason, the textbox ALWAYS stole the focus from everything...
  • Tim Schmelter
    Tim Schmelter about 11 years
    This works also for container controls like panels. I just wanted to remove focus completely and it worked: this.ActiveControl = panelOnMyForm;
  • joelc
    joelc about 11 years
    Great suggestion. This solved my problem. I'm using KeyPress against the form itself and have several buttons etc. Problem is that the app is taking input from a cardreader, so if focus goes away from the form itself then all hell breaks loose. Having focus on a label after a button-click solved my problem. Thanks so much!
  • Rostov
    Rostov almost 10 years
    I believe this is the best answer. A lot of the other methods like "Focus" if you read MSDN, are listed as low level methods for control designers. If you want everything else to be "not selected" this appears to be the easiest approach since, well, it's just one small line.
  • Derek W
    Derek W over 9 years
    Tried everything else here and Select() worked for me.
  • LeftyCoder
    LeftyCoder over 8 years
    Exactly the kind of solution I was looking for. Thanks!
  • miguelmpn
    miguelmpn over 8 years
    How can I disable it?
  • Raktim Biswas
    Raktim Biswas over 7 years
    @miguelmpn textBox1.Enabled = false; will disable your textbox. and setting it to true will re-enable it.
  • Panzercrisis
    Panzercrisis over 7 years
    This may be half the reason Microsoft added this property in the first place.
  • makoshichi
    makoshichi about 7 years
    I hope people scroll down all the way to this and don't just use the workaround marked as answer
  • xoxel
    xoxel over 5 years
    Still the best answer to this day, here buddy, take my upvote.
  • NetWave
    NetWave about 4 years
    This seems like the most elegant solution, it worked perfect in my case.
  • Rhys Jones
    Rhys Jones almost 4 years
    I really want this answer to work because it just seems like it should, but in my case it didn't work because whilst it did trigger the Leave event it did not trigger the Validating/Validated events.
  • Tyler2P
    Tyler2P about 3 years
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
  • Special Sauce
    Special Sauce about 3 years
    This is certainly the simplest and most effective solution. You could add this line to the Form's Activated event and prevent the child textbox from automatically selecting all of the text.