How do you know when a user clicked out of your control?

I have a custom UserControl. I want to use it in several different products, so I want something that can be implemented inside UserControl itself. I want to know when a user went beyond UserControl so that I can hide it, like a ComboBox. How can i do this?

I tried to handle the click event, but it only fires if the click occurred within the control.

+3
source share
3 answers

What is intended for the Capture property. Set it to true and all mouse messages will be redirected to your control, even if it goes outside the window. Check the e.Location property in the MouseDown event.

+3
source

Hm, you can do what you want by listening to the GotFocus / LostFocus events. The combo box gives the focus a lowering when they open and close them when they lose focus.

+1
source

  • ,
  • MouseClick
  • Common_MouseClick

:

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

Source: https://habr.com/ru/post/1774660/


All Articles