Hi all,

I'm using the following code at the moment to get a panel to drag across the form:

private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                
                Point newLoc = new Point(e.X+panel1.Location.X, e.Y+panel1.Location.Y);
                panel1.Location = newLoc;
                
            }
        }

I had to put the +panel1.Location.X parts in there otherwise it flashes twice on the screen when moving.

The method I am using is working fine, but it always snaps to the top left hand corner of the panel.

Any ideas how I can get around this?

Thanks

Try this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmPanel : Form
  {
    private Point start;

    public frmPanel()
    {
      InitializeComponent();
    }

    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
      if (e.Button == MouseButtons.Left)
      {
        start = e.Location;
        panel1.MouseUp += new MouseEventHandler(panel1_MouseUp);
        panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);
      }
    }

    void panel1_MouseUp(object sender, MouseEventArgs e)
    {
      panel1.MouseMove -= new MouseEventHandler(panel1_MouseMove);
      panel1.MouseUp -= new MouseEventHandler(panel1_MouseUp);
    }

    void panel1_MouseMove(object sender, MouseEventArgs e)
    {
      panel1.Location = new Point(panel1.Location.X-(start.X-e.X), panel1.Location.Y-(start.Y - e.Y));
    }
  }
}

Assign the MouseDown event in the IDE. The rest of the events are handled in code.

Oh and to stop them from moving the panel off of the form look in to Cursor.Clip so you can constrain the movement of the cursor to an area that is on the form. That gets a little more complicated ;)

You can generalize the code I posted above to use a Control reference instead of the panel so you can, BeginDragging(Control), Dragging(Control), EndDragging(Control) so you can reuse the same code for all controls on the form. Just a thought.

Comments
You give my some new ideas on something I am struggling with for some time now. Thanks!

on Mouse down, capture the current mouse & panel position.

Then on Mouse move, calculate the delta mouse position. Then calculate the new panel position based on the delta mouse and captured panel position.

Thanks everyone! I got it working really well. i'll give the Cursor.Clip a go too. Do you have a quick example of just restricting the movement to the form (so the edges can't go past, etc)?

Can you do this sort of thing in asp easily too?

That is an entirely different subject. It is possible but I don't know how to go about.

Please mark this thread as solved if you have found a solution to your question and good luck!

That is an entirely different subject. It is possible but I don't know how to go about.

Please mark this thread as solved if you have found a solution to your question and good luck!

Will do and thanks again!

Just use this code

//In my case, I want to drag a label around a panel.
Cursor.Clip = new Rectangle(panel.location, panel1.size);

Hope this helps

UPDATE don't use (in this example) only "panel1.Location" use "form1.Location + panel1.Location" Like this:

Cursor.Clip = new Rectangle(form1.Location + panel.location, Panel.Size);

Sorry for keep changi the code it's because I wrote before testing it.
This is the final (working) one:

Cursor.Clip = new Rectangle(this.Location.X + panel1.Location.X + 8, this.Location.Y + panel1.Location.Y + 28, panel1.Size.Width, panel1.Size.Height);

The + 8 and + 28 is because this.Location doesn't count the form border. the values 8 for X and 28 for Y stand for a default windows vista form.

Edited 6 Years Ago by Matt_Lebrao: Update

This question has already been answered. Start a new discussion instead.