Question

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!

Was it helpful?

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;
    }

OTHER TIPS

Ok, the good news: hopefully we'll have a CSI desktop layout once you're done.

The not so good news: I think you're trying to do the wrong thing.

I'm assuming you're using WinForms, but the idea should be the same for WPF. When you run your program, the layout is set (either dynamically, for example it changes when you resize the window, or statically, for example when you set it with absolute X,Y position).

What you want to do is kind of: I want to get the benefits of them creating all the boiler plate code, but then, I want to dynamically change it at run time ...

I think you need to rethink your approach, and take this one step at a time (after all, it took Microsoft years to get the GUI to where it's at at the moment ... It'll take some time to get the cool futuristic GUI's out).

Take baby steps, but keep in mind the big picture:

  • Start with having a Canvas defined.
  • Then you can place a button on it.
  • Then fool around with moving that button on the canvas. (The code you have in your question should work for an object on a canvas).

From there, keep taking steps that will get you closer to your destination ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top