Click here to Skip to main content
15,881,831 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
How i can move 2 user controls at same time ? I know move only 1 per 1... Any example or idea ?

To move 1:

C#
private void button1_MouseDown(object sender, MouseEventArgs e)
{
    btnStandard = (Button)sender;


        if (!this.dragInProgress)
        {
            this.dragInProgress = true;
            this.MouseDownX = e.X;
            this.MouseDownY = e.Y;
        }
        return;

}

private void button1_MouseMove(object sender, MouseEventArgs e)
{
    if (dragInProgress)
    {
        Point temp = new Point();
        temp.X = this.btnStandard.Location.X + (e.X - MouseDownX);
        temp.Y = this.btnStandard.Location.Y + (e.Y - MouseDownY);

        this.btnStandard.Location = temp;
    }
}

private void button1_MouseUp(object sender, MouseEventArgs e)
{
    btnStandard = (Button)sender;

        if (e.Button == MouseButtons.Left)
        {
            this.dragInProgress = false;
        }
        return;

}
Posted
Updated 23-Oct-13 2:30am
v2
Comments
BillWoodruff 23-Oct-13 9:58am    
I upvoted this question because I think it's an inherently interesting programming challenge in WinForms. But, when I say "challenge," I don't mean so difficult you have to be a very advanced programmer to write the code that implements it ! I mean "challenge" in the sense that knowing how to solve a problem like this requires you to have some mastery of Events, and Controls.

Before I respond, I'd like to ask you a few questions that will help clarify for me what functionality you want to create:

1. are you looking to "entrain" any number of Controls so that moving any one of them moves all the others the same distance on X, and Y ?

2. or, are you looking to have one Control that can be moved, and then other Controls that "follow" the movement of the Control that can be moved, but that cannot be moved themselves ?

3. are you looking for making your Controls movable as your Application starts, and leaving them them movable; or, do you want the ability, for specific Controls, to "turn off" being movable at any point while the Application is running ?

4. Do you want the end-user, to be able to, at run-time, "turn off" other Controls moving in synch with the Control, but still leave the Control movable ?

5. finally: do you wish to have a solution that moves, in synch, Controls of several different Types, or, are all the Controls to be moved of the same Type; for example: all Panels.
johannesnestler 23-Oct-13 11:03am    
good points - as I have seen sometimes in your answers - you often think beyond the single requirment. I like your approach and I don't like that your comment can't be upvoted ;-)
BillWoodruff 23-Oct-13 11:08am    
Thank you very much, Johannes ! That kind of feedback means a lot to me.

bill
Rieth L 23-Oct-13 11:03am    
1
Alexander Dymshyts 23-Oct-13 11:17am    
What kind of userControls do you have? Can you put this 2 userControls to one panel for example and move that panel instead?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900