1

I have created a Winforms custom control, and while it can receive MouseWheel messages just fine, none of MouseDown, MouseMove, or MouseUp messages are received.

I have set the following ControlStyles:

SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint, true);

(I have also tried setting ControlStyles.Selectable and ControlStyles.UserMouse, but it makes no difference)

I have put code in the GotFocus and LostFocus events, and I can see that in some circumstances the control does get focus, only to lose it again immediately.

This is a GIS map viewer / editor control, and it must be able to receive focus, both so that it can receive mouse events, but also so that I can use hotkeys to perform various actions.

Any ideas?

[Edit: Here's some code which demonstrates this. This question is I guess identical to Setting Focus to a .NET UserControl...?, which wasn't answered satisfactorily. ]

public partial class CustomControl1 : Control
{
    public CustomControl1()
    {
        SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.Selectable | ControlStyles.UserMouse, true);
        MouseWheel += CustomControl1_MouseWheel;
        MouseDown += CustomControl1_MouseDown;
        MouseMove += CustomControl1_MouseMove;
        MouseUp += CustomControl1_MouseUp;
    }

    void CustomControl1_MouseUp(object sender, MouseEventArgs e)
    {
        Debug.Write("MouseUp"); // never gets fired
    }

    void CustomControl1_MouseMove(object sender, MouseEventArgs e)
    {
        Debug.Write("MouseMove"); // never gets fired
    }

    void CustomControl1_MouseDown(object sender, MouseEventArgs e)
    {
        Debug.Write("MouseDown"); // never gets fired
    }

    void CustomControl1_MouseWheel(object sender, MouseEventArgs e)
    {
        Debug.Write("MouseWheel"); // works fine!
    }

    protected override void OnPaint(PaintEventArgs pe)
    {
        // do some drawing stuff here
    }
}
Community
  • 1
  • 1
  • `the control does get focus, only to lose it again immediately`. Using a timer somewhere? Instead of using events, override the methods, like OnMouseDown, etc. Your question isn't really answerable since it requires code to look at. – LarsTech Jul 25 '13 at 15:30
  • No timers are being used anywhere in the application. I have also tried overriding OnMouseDown etc, and they don't get fired. I think the answer I'm looking for will involve enabling a custom control to receive focus. – Curtis Burisch Jul 25 '13 at 15:38
  • 2
    Try posting code that reproduces the problem. – LarsTech Jul 25 '13 at 15:48
  • Are you using a UserControl or have you subclassed an existing control such as the Panel control? In C# projects I have used a subclassed Panel control which works fine for a viewer/editor. Just override the OnNNN events that you want to handle. – deegee Jul 25 '13 at 17:38
  • I have derived from Control. Overriding the OnMouseMove etc does not work. – Curtis Burisch Jul 26 '13 at 09:01
  • SOLUTION : Well, not solved, but I have tracked down the problem. I am using the WeifenLui.WinFormsUI docking system, and it appears there's a bug in this that prevents focussing. – Curtis Burisch Jul 26 '13 at 09:30

0 Answers0