Link to home
Start Free TrialLog in
Avatar of aceswildab1
aceswildab1

asked on

MouseDown Event Not firing on user control

I'm new to working with user controls in C#. I have created a small user control that consists of a panel, picture, and label. I want to be able to drag and drop the user control. I have the following code:

        private void imageContainer1_MouseDown(object sender, MouseEventArgs e)
        {
            imageContainer1.DoDragDrop(imageContainer1, DragDropEffects.All);
        }

Open in new window


The problem I am having is that the MouseDown event is not firing off on the user control. Is there something that I am missing? Am I not able to drag/drop the entire control like any other control? Thanks for your help.
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

The MouseDown() event will fire for whatever control your are over so if the mouse is over the Panel then the Panel will get the MouseDown() event.  If the mouse is over the PictureBox then the PictureBox will get the event, etc...

If you want to be able to move it from anywhere within the UserControl then you need to wire up ALL the controls so that they either fire that existing handler or they all have their own handlers that call similar code.
Did you subscribe to the MouseDown event using the event handler you created? See code below.

On the other hand, I'm not entirely sure you can actually drag a control, but I could be wrong. I know you can drag an object from a ListBox or from Windows Explorer onto a control. But dragging a control...I don't know.
this.imageContainer1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.imageContainer1_MouseDown);

Open in new window

Avatar of aceswildab1
aceswildab1

ASKER

I know that it is possible to move an entire control. I've been testing it out with other controls. I also do have the MouseDown event for the user control hooked up correctly. The code is in place.

Idle, are you saying that within my user control, I would need to enable the drag/drop code for each and every control within it to get the user control itself to drag? The user control wouldn't just drag and take everything that it contains with it?
I've set the mousedown events on the controls within the user control. This allows me to do a drag/drop with those controls within it. Is there a way of doing drag/drop with the entire user control as a whole or will it only work with the controls within it? (IE - allowing me to move the entire control rather than the individual parts within)
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's exactly what I needed. It works just the way I was wanting it to. Thanks for the help!!!