开发者

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

开发者 https://www.devze.com 2023-01-24 17:04 出处:网络
I have a custom UserControl. I want to u开发者_如何学编程se 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 h

I have a custom UserControl. I want to u开发者_如何学编程se 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;
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消