Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MouseDown Event Not firing on user control

Posted on 2011-03-11
6
Medium Priority
?
2,095 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 86

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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 

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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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