esac
esac

Reputation: 24695

How to tell when the user has clicked outside of the bounds your control?

I have a custom UserControl. I want to use it in a few different products, so I want something that can be implemented inside of the UserControl itself. I want to know when the user has clicked outside of the bounds of the UserControl so that I can hide it, similar to a ComboBox. How can I do that?

I tried handling the click event, but it only seems to fire if the click occured within the bounds of the control.

Upvotes: 3

Views: 1505

Answers (3)

Hans Passant
Hans Passant

Reputation: 942548

That's what the Capture property is designed to do. Set it to true and all mouse messages are routed to your control, even if it moves out of the window bounds. Check the e.Location property in the MouseDown event.

Upvotes: 3

Javed Akram
Javed Akram

Reputation: 15354

do this

  • Select all controls on your form including form
  • In Property Window select MouseClick event
  • Now enter below Code in Common_MouseClick

Code:

 if (!sender.Equals(yourControl))
  {
        yourControl.Visible=false;
  }

Upvotes: -1

scmccart
scmccart

Reputation: 1169

Hm, you may be able to accomplish what you want by listening to the GotFocus/LostFocus events. ComboBoxes give the drop downs focus when they open and close them when they lose focus.

Upvotes: 1

Related Questions