Moving a control by dragging it with the mouse in C#
Asked Answered
B

3

18

I'm trying to move the control named pictureBox1 by dragging it around. The problem is, when it moves, it keeps moving from a location to another location around the mouse, but it does follow it... This is my code. and I would really appreciate it if you could help me

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    bool selected = false;
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        selected = true;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (selected == true)
        {
            pictureBox1.Location = e.Location;
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        selected = false;
    }

}
Bethannbethanne answered 5/5, 2013 at 13:14 Comment(0)
I
47

All you need:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }


    private Point MouseDownLocation;


    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            MouseDownLocation = e.Location;
        }
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            pictureBox1.Left = e.X + pictureBox1.Left - MouseDownLocation.X;
            pictureBox1.Top = e.Y + pictureBox1.Top - MouseDownLocation.Y;
        }
    }

}
Illboding answered 5/5, 2013 at 13:25 Comment(5)
@TurmDrummer You should use english on this site (at least try to) for other users to understand you. Related discussion: meta.stackexchange.com/questions/118678/…Illboding
I am sorry, I had not realized, that I wrote my comment in german. Sometimes I am not aware in which language I am communicating. My Question was, since you seem to have a good knowledge with this kind of UI-Code, do you have an idea how I could smooth the movement of the dragged control a little bit? If there is no practical solution with this way of implementation, I will rather stick to your approch, because the other solutions I found were often bad written, flawed or impractical and your solution works fine right now.Nealneala
@Nealneala Even if I have good knowledge of this (I havn't), and even if I've got ideas of what are you asking about (I havn't), why don't you create a new question?Illboding
pictureBox1.Left = (-1 * e.X) + pictureBox1.Left + MouseDownLocation.X; If anyone needs inverted scrolling.Concubinage
Thank you very much astef, loved this answer, saved me a lot of time. I made the MouseMove generic for a control by casting the sender so I could use the same code for several controls, the MouseDown already works for multiple controls. Control control = (Control)sender; control.Left = e.X + control.Left - MouseDownLocation.X; control.Top = e.Y + control.Top - MouseDownLocation.Y;Metamorphic
K
10

You can also use the extension:

public static class CmponentsExtensions
{
  //Management of mouse drag and drop
    #region Menu and Mouse
    private static bool mouseDown;
    private static Point lastLocation;

    /// <summary>
    /// To enable control to be moved around with mouse
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="control"></param>
    public static void moveItselfWithMouse<T>(this T control) where T: Control
    { 
        control.MouseDown += (o, e)=> { mouseDown = true; lastLocation = e.Location; };
        control.MouseMove += (o, e) => 
        {
            if (mouseDown)
            {
                control.Location = new Point((control.Location.X - lastLocation.X) + e.X, (control.Location.Y - lastLocation.Y) + e.Y);
                control.Update();
            }
        };
        control.MouseUp += (o, e) => { mouseDown = false; } ;
    }


    public static void moveOtherWithMouse<T>(this T control, Control movedObject) where T : Control
    {
        control.MouseDown += (o, e) => { mouseDown = true; lastLocation = e.Location; };
        control.MouseMove += (o, e) =>
        {
            if (mouseDown)
            { 
                movedObject.Location = new Point((movedObject.Location.X - lastLocation.X) + e.X, (movedObject.Location.Y - lastLocation.Y) + e.Y);
                movedObject.Update();
            }
        };
        control.MouseUp += (o, e) => { mouseDown = false; };
    }

    #endregion
}

Then you need to use it with some control:

In this case pictureBox1 moved the whole Form

pictureBox1.moveOtherWithMouse(this);

In this case you move only pictureBox:

pictureBox1.moveItselfWithMouse();
Karykaryl answered 1/6, 2019 at 11:15 Comment(2)
@PawwlWojda Neither e nor control have Location as a property.Oulu
SezMe are you using Windows Forms or WPF??Faria
N
6

try this to move pictureBox control at runtime using mouse

 private void pictureBox7_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                xPos = e.X;
                yPos = e.Y;
            }
        }

        private void pictureBox7_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            PictureBox p = sender as PictureBox;

            if (p != null)
            {
                if (e.Button == MouseButtons.Left)
                {
                    p.Top += (e.Y - yPos);
                    p.Left += (e.X - xPos);
                }
            }

        }
Nielson answered 21/3, 2015 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.