Solved

MouseDown Event Not firing on user control

Posted on 2011-03-11
6
1,900 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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