views:

142

answers:

1

I have a textbox in a windows form. Currently the focus is on the textbox and i enter some text. Now I click outside the textbox but within the window. This action does not make the text box to lose the focus. The cursor still blinks in the text box. If the click was on another control then the text box would lose the control.

How would I make the text box to lose control when clicked outside of it (not just on another control but anywhere inside the form)?

Thanks in advance.

Datte

+3  A: 

Because you click on a control that has no ability of taking the focus (like a form, a label, etc). If you click for instance on another text box the focus should move..

To move the focus programmatically (i.e. in the OnClick event of the Form) use the control.Focus method.

munissor
Have a look at the Control.CanFocus property documentation http://msdn.microsoft.com/en-us/library/system.windows.forms.control.canfocus(v=VS.100).aspx
munissor
This is not working for me. The Form.CanFocus returns true. In the OnClick or MouseClick event i explicitely write this.Focus() [even tried this.Select()], the focus is still on the textbox. Any other solution?
dattebayo
Having the focus on a form has no sense. Maybe your text box is the only control in the form? or it has the lowest TabIndex inside the form? Another possibility can be that you have a Validate event for your text box and the validation fails, so the text box doesn't loose the focus.
munissor