Search code examples
c#windowsvisual-studio-express

C# Can't drag my button with mouse


So i searched a little around on google to find some code to drag buttons with the mouse. I found a lot, even though none of these actually worked for me.

So i'm asking you! :-)

The code i'm trying to get working in my form:

bool isDragged = false;
  Point ptOffset;
  private void button1_MouseDown( object sender, MouseEventArgs e )
  {
     if ( e.Button == MouseButtons.Left )
     {
        isDragged = true;
        Point ptStartPosition = button1.PointToScreen(new Point(e.X, e.Y));

        ptOffset = new Point();
        ptOffset.X = button1.Location.X - ptStartPosition.X;
        ptOffset.Y = button1.Location.Y - ptStartPosition.Y;
     }
     else
     {
        isDragged = false;
     }
  }

  private void button1_MouseMove( object sender, MouseEventArgs e )
  {
     if ( isDragged )
     {
        Point newPoint = button1.PointToScreen(new Point(e.X, e.Y));
        newPoint.Offset(ptOffset);
        button1.Location = newPoint;
     }
  }

  private void button1_MouseUp( object sender, MouseEventArgs e )
  {
     isDragged = false;
  }

I of course changed the pictureBox1 to button1.

But i just can't get this to work.

Anyone who might know why?

Oh, and i want to use this at all my buttons, so what should i replace button1 with to make it work at all of the buttons?

-Btw, i use Visual studio Express.

Thank you in advance!


Solution

  • even though i'm really just copy pasting when it comes to windows forms

    The code you posted won't actually do anything by itself!

    To work, the MouseDown() and MouseMove() events of the control have to be wired up to those methods:

    1. Select the control on the form (pictureBox1).
    2. In the Properties Pane (bottom right by default), click the "Lightning Bolt" icon to get a list of the events for that control.
    3. Find the MouseDown entry and change the dropdown to the right of it to pictureBox1_MouseDown.
    4. Find the MouseMove entry and change the dropdown to the right of it to pictureBox1_MouseMove.

    Now run it and drag the pictureBox1 around.

    EDIT: Here is how to make the code work for multiple controls, as outlined in my comment below.

        bool isDragged = false;
        Point ptOffset;
    
        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDragged = true;
                Button btn = (Button)sender;
                ptOffset = new Point(btn.Location.X - Cursor.Position.X, btn.Location.Y - Cursor.Position.Y);
            }
            else
            {
                isDragged = false;
            }
        }
    
        private void button1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragged)
            {
                Point newPoint = Cursor.Position;
                newPoint.Offset(ptOffset);
                Button btn = (Button)sender;
                btn.Location = newPoint;
            }
        }
    
        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            isDragged = false;
        }