Do I really have to call Focus in OnMouseDown of my custom Control?

Posted by Thomas on Stack Overflow See other posts from Stack Overflow or by Thomas
Published on 2010-03-27T12:29:01Z Indexed on 2010/03/27 12:33 UTC
Read the original article Hit count: 203

Filed under:
|
|
|

I'm implementing a custom control that inherits from Control. I want it to be focusable (it's a kind of list box).

In the constructor, I do

SetStyle(ControlStyles.Selectable, true);

I can now use Tab to navigate to the control.

However, when the control receives a mouse click, it does not automatically claim focus. I can work around this, of course:

protected override void OnMouseDown(MouseEventArgs e)
{
    Focus();
    base.OnMouseDown(e);
}

But this feels like a kludge that should not be necessary. Is this really the way to go? Or is there some way to tell Control to claim focus automatically when it receives a mouse click?

© Stack Overflow or respective owner

Related posts about winforms

Related posts about .NET