views:

444

answers:

2

I'm making a custom control with a panel. I want to be able to drag and drop it so I've implemented that in the MouseDown event of my control. But I want the thing to react when you start drag to give a little feedback to the user. So in the MouseDown even I change the color. Then I want to change it back in the MouseUp event.

My control is not installed into VS2008 but just a class I've written that I instanciate at run time (I don't know in advance how many I need and so on). Now, my control exposes a MouseDown event so as to be able to be dragged. When I subscribe to this event from the parent application to actually perform the drag and drop my control is not repainted on its MouseUp event! In fact, the MouseUp is never invoked. If, on the other hand, I don't subscribe to the event in the parent app it works as intended.

What's going on? Is the parent interrupting the flow so that the MouseUp event never fires in my control? How do I get around this?

A: 

Do you capture the mouse in the custom control on the mousedown event? Try capturing on the mousedown and releasing the capture on the mouseup.

Jarrod
What do you mean by capture?
Niklas Winde
+1  A: 

I'm not sure if you are using Windows Forms or WPF, but in Windows forms here is what I mean:

public class DerivedPanel : Panel
{
    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e);
        Capture = true;
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        base.OnMouseUp(e);
        Capture = false;
        // Change your color or whatever here
    }
}

In WPF there are two methods, CaptureMouse() and ReleaseMouseCapture() to do the same thing. When the control captures the mouse, it will received mouse events even if the cursor isn't over the control. This could be causing your problem. See MSDN Article

Jarrod
It's a normal Windows form. Thanks for your response -I'll try that during the weekend and if I don't succeed I'll post some code of what I'm doing.
Niklas Winde