UI and Focus-based Events

I have a usercontrol plugin written in vb.net and I need an event fired whenever the control loses focus (e.g user clicks off my control into the model space in Rhino). I’ve tried using the UserControl.Leave event to no avail.

Is this possible to do?

I’m using RhinoCommon.



What about the UserControl.LostFocus event?


Thanks for the suggestion but it doesn’t fire either.

How about MouseLeave?

@AIW - Interesting.

This fires when the mouse cursor leaves the control, so I could use it to set a flag isMouseOverControl (I can un-set it with UserControl.MouseEnter)

But I would still need an event that fires when the mouse is clicked, regardless of where on screen the mouse cursor is. Haven’t come across such a thing, but I’ll look see what’s out there.

Also, feels like this is getting messy - i’m not sure what weirdness would be caused by situations like popup dialog boxes, etc.

How do you show your control? Is it in a Rhino Panel, or in a “default” Winforms Window?
Is InitializeComponent() called in the constructor? Make sure that it is.

There’s a whole bunch of mouse events including mouse leave, mouse wheel, mouse click, etc. If you are using Visual Studio, just pull down the RH list above the edit window while the left window has your control selected.

Thanks this is a Rhino Panel. I’m having a few issues getting your code working menno:

  • I need to import system.windows.interop for the HwndSource class correct?
  • Where is loaded declared? How do I translate “Loaded += delegate” to vb.net
  • HwndSource.FromVisual expects a System.Windows.Media.Visual object, not UserControl.


Ah, I see now that I have mixed things up with WPF code. I will remove the code, that is not good.

I ask again: have you called InitializeComponent and have you called it before you subscribe to the events?

I can hook up the LostFocus and GotFocus events and have them triggered when the mouse clicks on the control (GotFocus) and when the mouse clicks on Rhino (LostFocus).

I have noticed, though, that when the UserControl contains other elements, like for instance a text box, and you click in the textbox and then again in Rhino that the UserControl’s LostFocus event does not fire. For that you have to subscribe to the TextBox’s LostFocus event!

In my test the events fire if I subscribe to them.

Thanks menno - the designer (autogenerated) code seems to call initializecomponent() in the constructor. I did notice controls within the UserControl firing on GotFocus and LostFocus. So a workable solution is to handle all these events within one function which checks for the condition that all controls have no focus, and then execute the behavior I’m after.

Thank you!