How can I lose focus on a control when I click outside it?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kirk kirk
    New Member
    • Dec 2010
    • 4

    How can I lose focus on a control when I click outside it?

    I have a form that contains text boxes and combo boxes. My problem is that when I click on a control, let's say a comboxbox, I can edit inside it but when I click outside, the main form where my controls are contained, my focus is still in the combobox.
    Last edited by kirk kirk; Apr 21 '11, 06:19 AM. Reason: wrong spelling
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    If I'm understanding you correctly, you want your controls to lose focus when you click on the form itself? There might be a better way to do this, but here's what I came up with.

    Add a listener to the form's MouseClick event. Inside that method, put the following code...

    Code:
    this.ActiveControl = null;
    This should clear the active control for the form and remove focus from your controls.

    If that's not what you meant, can you please clarify?

    Comment

    • kirk kirk
      New Member
      • Dec 2010
      • 4

      #3
      Sorry if I did not clarify things up. Even I find it hard to explain it further. I have this combobox with AutoCompleteMod e set to SuggestAppend.

      My problem -> I click on the combobox and choose an item. Then I click the form and use my mouse wheel to scroll the whole form but what I'm scrolling here is not the whole form as to what I intend to do but just the items inside the combobox.

      The same goes when I click on a textbox inside a tablelayoutpane l which is inside a groupbox.

      I assume you had the idea because your code helped me but now every time I click my main form it scrolls back to where I have my focus. I want my form in a way that I have my form scrolled to where I'm at and just have to click the main form whenever I want to scroll up or down.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        And that suggestion didn't work? What I was trying to do was take focus away from the control itself and put it on the form. I didn't know you wanted to scroll the form, but it should have the same result.

        No go?

        Comment

        Working...