Draggable Panel

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2008
Posts: 10
Reputation: nlblnx is an unknown quantity at this point 
Solved Threads: 0
nlblnx nlblnx is offline Offline
Newbie Poster

Draggable Panel

 
0
  #1
Jul 23rd, 2009
Hi all,

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

  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,254
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 579
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Draggable Panel

 
0
  #2
Jul 23rd, 2009
Try this:
  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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,254
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 579
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Draggable Panel

 
1
  #3
Jul 23rd, 2009
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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: dlhale is an unknown quantity at this point 
Solved Threads: 1
dlhale dlhale is offline Offline
Newbie Poster

Re: Draggable Panel

 
0
  #4
Jul 23rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 10
Reputation: nlblnx is an unknown quantity at this point 
Solved Threads: 0
nlblnx nlblnx is offline Offline
Newbie Poster

Re: Draggable Panel

 
0
  #5
Jul 23rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,254
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 579
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Draggable Panel

 
0
  #6
Jul 23rd, 2009
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!
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 10
Reputation: nlblnx is an unknown quantity at this point 
Solved Threads: 0
nlblnx nlblnx is offline Offline
Newbie Poster

Re: Draggable Panel

 
0
  #7
Jul 23rd, 2009
Originally Posted by sknake View Post
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC