943,526 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 3058
  • C# RSS
Jul 23rd, 2009
0

Draggable Panel

Expand Post »
Hi all,

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

C# Syntax (Toggle Plain Text)
  1. private void panel1_MouseMove(object sender, MouseEventArgs e)
  2. {
  3. if (e.Button == MouseButtons.Left)
  4. {
  5.  
  6. Point newLoc = new Point(e.X+panel1.Location.X, e.Y+panel1.Location.Y);
  7. panel1.Location = newLoc;
  8.  
  9. }
  10. }

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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nlblnx is offline Offline
10 posts
since Aug 2008
Jul 23rd, 2009
0

Re: Draggable Panel

Try this:
c# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace daniweb
  11. {
  12. public partial class frmPanel : Form
  13. {
  14. private Point start;
  15.  
  16. public frmPanel()
  17. {
  18. InitializeComponent();
  19. }
  20.  
  21. private void panel1_MouseDown(object sender, MouseEventArgs e)
  22. {
  23. if (e.Button == MouseButtons.Left)
  24. {
  25. start = e.Location;
  26. panel1.MouseUp += new MouseEventHandler(panel1_MouseUp);
  27. panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);
  28. }
  29. }
  30.  
  31. void panel1_MouseUp(object sender, MouseEventArgs e)
  32. {
  33. panel1.MouseMove -= new MouseEventHandler(panel1_MouseMove);
  34. panel1.MouseUp -= new MouseEventHandler(panel1_MouseUp);
  35. }
  36.  
  37. void panel1_MouseMove(object sender, MouseEventArgs e)
  38. {
  39. panel1.Location = new Point(panel1.Location.X-(start.X-e.X), panel1.Location.Y-(start.Y - e.Y));
  40. }
  41. }
  42. }

Assign the MouseDown event in the IDE. The rest of the events are handled in code.
Last edited by sknake; Jul 23rd, 2009 at 3:46 pm.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 23rd, 2009
1

Re: Draggable Panel

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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 23rd, 2009
0

Re: Draggable Panel

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.
Reputation Points: 12
Solved Threads: 1
Newbie Poster
dlhale is offline Offline
10 posts
since Nov 2008
Jul 23rd, 2009
0

Re: Draggable Panel

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?
Last edited by nlblnx; Jul 23rd, 2009 at 7:07 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nlblnx is offline Offline
10 posts
since Aug 2008
Jul 23rd, 2009
0

Re: Draggable Panel

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!
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 23rd, 2009
0

Re: Draggable Panel

Click to Expand / Collapse  Quote originally posted by sknake ...
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nlblnx is offline Offline
10 posts
since Aug 2008
Sep 22nd, 2010
0

Cursor Clipping

Just use this code

C# Syntax (Toggle Plain Text)
  1. //In my case, I want to drag a label around a panel.
  2. Cursor.Clip = new Rectangle(panel.location, panel1.size);

Hope this helps
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Matt_Lebrao is offline Offline
3 posts
since Sep 2010
Sep 22nd, 2010
0

Cursor Clipping

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

C# Syntax (Toggle Plain Text)
  1. Cursor.Clip = new Rectangle(form1.Location + panel.location, Panel.Size);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Matt_Lebrao is offline Offline
3 posts
since Sep 2010
Sep 22nd, 2010
0

UPDATE

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

C# Syntax (Toggle Plain Text)
  1. 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.
Last edited by Matt_Lebrao; Sep 22nd, 2010 at 2:18 am. Reason: Update
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Matt_Lebrao is offline Offline
3 posts
since Sep 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: need help please
Next Thread in C# Forum Timeline: change color of shadow of popup contain ajex. in C#.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC