954,160 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Draggable Panel

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

nlblnx
Newbie Poster
10 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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.

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

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.

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

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.

dlhale
Newbie Poster
10 posts since Nov 2008
Reputation Points: 12
Solved Threads: 1
 

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?

nlblnx
Newbie Poster
10 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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!

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

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!

nlblnx
Newbie Poster
10 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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

Matt_Lebrao
Newbie Poster
3 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

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);
Matt_Lebrao
Newbie Poster
3 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

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.

Matt_Lebrao
Newbie Poster
3 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You