Is there a way to make a UserControl unfocussable?
EDIT:
So SetStyle(ControlStyles.Selectable, false)
is the way to go. But still there is difference to Control
. If you inherit form Control
the initial control does not lose focus. But after clicking on your control that is derived from UserControl
and
ControlStyles.Selectable
is applied focus is removed from initial control.
In your constructor after InitializeComponent()
you need to call SetStyle
and set the ControlStyles.Selectable
style to false
:
SetStyle(ControlStyles.Selectable, false);
Besides ControlStyles.Selectable there is also a ControlStyles.ContainerControl - the documentation is rather sparse on this topic (If true, the control is a container-like control), but it somehow affects if the child controls get focus instead of the control itself.
EDIT:
I have just noticed another interesting fact. Viewing a UserControl in reflector shows that it forces setting the input focus in OnMouseDown. So overriding OnMouseDown without calling base.OnMouseDown(e) resolves the issue with no side-effects.
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected override void OnMouseDown(MouseEventArgs e)
{
if (!this.FocusInside())
{
this.FocusInternal();
}
base.OnMouseDown(e);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With