| | |
Draggable Panel
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 10
Reputation:
Solved Threads: 0
Hi all,
I'm using the following code at the moment to get a panel to drag across the form:
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
I'm using the following code at the moment to get a panel to drag across the form:
C# Syntax (Toggle Plain Text)
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:
Assign the MouseDown event in the IDE. The rest of the events are handled in code.
c# Syntax (Toggle Plain Text)
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.
Last edited by sknake; Jul 23rd, 2009 at 3:46 pm.
Oh and to stop them from moving the panel off of the form look in to 
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.
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.
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!
Please mark this thread as solved if you have found a solution to your question and good luck!
![]() |
Similar Threads
- Making a graphics component draggable (Java)
- Help! Can't browse, use control panel or Explorer (Viruses, Spyware and other Nasties)
- Intel mainboard D845HV, front panel configuration (Motherboards, CPUs and RAM)
- Drawing A Table On A Container(Panel) (Java)
- ActionListeners on the components of a panel (Java)
- Problems in Win98SE with Desktop Shortcuts and The Control Panel (Windows 95 / 98 / Me)
Other Threads in the C# Forum
- Previous Thread: how to sort dictionary?
- Next Thread: Program Won't Work With Vista
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# cast check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener mailmerge mandelbrot math mouseclick mysql operator path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox robot save saving serialization server sleep socket sockets sql sql-server statistics stream string stringformatting sun table tcp text textbox thread time timer update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






