Solved

MouseDown Event Not firing on user control

Posted on 2011-03-11
6
1,784 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:aceswildab1
  • 3
  • 2
6 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35113396
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.
0
 
LVL 8

Expert Comment

by:dericstone
ID: 35122974
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

0
 

Author Comment

by:aceswildab1
ID: 35127230
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?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:aceswildab1
ID: 35127462
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)
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 35127855
Yes...you can drag it as a whole.  Wire up everything to the same handler and only call drag with the UserControl itself as the object being worked on.

Show me your code...
0
 

Author Closing Comment

by:aceswildab1
ID: 35128453
That's exactly what I needed. It works just the way I was wanting it to. Thanks for the help!!!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now