Remove focus if clicked anywhere else
10,571
Solution 1
Set the ActiveControl property of the form to null
to achieve what you want.
Note that this is contrary to what all other programs does, so it'll be a disconnect between the user expectations and the actual user experience. I would advice against it.
Solution 2
Something needs to get the focus. That something is what you should click. That better be another control on the form, the form itself doesn't want the focus. It has no use for it. Or click, say, the Start button.
Solution 3
Just put your controls in a panel and set your focus on an harmless control (Like a label) :
Private Sub Panel1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel1.Click
Label1.Focus()
End Sub
Author by
Saturn
Updated on June 13, 2022Comments
-
Saturn almost 2 years
I have a textbox, and I want it so if the user clicks anywhere on the form that is not the textbox itself, such textbox will lose focus. Any ideas?
-
Saturn about 13 yearsOkay, then, how do I check if a textbox was not clicked?
-
user1703401 about 13 yearsBizarre question. The absence of the Click event, I guess. What is this all about?
-
Brian A. Henning over 7 yearsWhat's missing from your answer (and thus makes it less useful for me, with a similar question) is when to set that property (i.e. what event). My form has toolbars, blah blah blah, and I don't want to have to hook special code into every single button handler to clear the focus of the textbox in question. Clicking a toolbar button does not naturally un-focus the textbox. :-(