简体   繁体   中英

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.

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.

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.

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;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM