views:

5412

answers:

3

I need to remove the focus from several textboxes; I tried using:

textBox.Focused = false;

but the property is read only. 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 focus from a textbox?

Thanks in advance.

+3  A: 

Try disabling and enabling the textbox.

Spencer Ruport
This works pretty slick as it automatically selects the next control in the tab list in the meantime.
Nick
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!
Albert Oldfield
+9  A: 

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 [Tab] to take it next.

Henk Holterman
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.
Callum Rogers
Container Controls (Form, Panel) will pass the Focus on to their first child control.
Henk Holterman
+1  A: 

Focus sets the input focus, so setting it to the form won't work because forms don't accept input. Try setting the form's ActiveControl property to a different control. You could also use Select to select a specific control or SelectNextControl to select the next control in the tab order.

Velociraptors