Matze
Matze

Reputation: 1584

Is there a way to make a UserControl unfocussable?

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.

Upvotes: 12

Views: 9812

Answers (7)

Bucket123
Bucket123

Reputation: 11

The answer from https://stackoverflow.com/users/1184950/tombam is the only correct one.

A UserControl sets focus to itself when processing its MouseDown event.

If the UserControl contains a selectable control then one of those is focused (not the UserControl itself). However, if the UserControl does not contain a selectable control then the UserControl itself receives focus. This happens even if you have done SetStyle(ControlStyles.Selectable, false); - it will still receive focus when clicked.

The only way to stop this is to override the OnMouseDown method and not call the base method - as described in the answer referenced.

Of course, if you need the MouseDown event then you will have to reimplement your own as the base UserControl.MouseDown event will no longer be fired.

Upvotes: 0

tombam
tombam

Reputation: 167

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

Upvotes: 6

ulatekh
ulatekh

Reputation: 1490

You can get closer to what you want by setting TabStop to false. That'll prevent the control from getting focused when, for instance, a dialog-box above its owning form closes.

Upvotes: 0

Ankit Mehta
Ankit Mehta

Reputation: 21

Yes, the SetStyle(ControlStyles.Selectable, false); works only if you are inheriting from a control.

It will not work, if you are inheriting from a user control.

To get around the problem, I added a panel to the user control and docked the panel to "Fill". Added rest of the controls to the panel instead of the user control. It worked!

Upvotes: 2

Adam Robinson
Adam Robinson

Reputation: 185593

In your constructor after InitializeComponent() you need to call SetStyle and set the ControlStyles.Selectable style to false:

SetStyle(ControlStyles.Selectable, false);

Upvotes: 14

JaredPar
JaredPar

Reputation: 754545

A UserControl, or any Control, will not be able to receive focus if the CanFocus property returns false. If you look at the code in reflector it basically checks 3 properties and if any are false then it will be un-focusable.

  • IsHandleCreated
  • IsVisible
  • IsEnabled

Setting the first two to false and having a functioning control is pretty much a contradiction. If it's possible though for your control to be functional with IsEnabled being false then that should work.

Upvotes: 1

Fredrik Mörk
Fredrik Mörk

Reputation: 158289

Enabled = false, perhaps?

Upvotes: 0

Related Questions