4
Answers

How can the user move a panel on a form with the cursor

Rob Cro

Rob Cro

14y
2.6k
1
I have a form which has a panel on it.  This form is a child of an MDI form.   The panel is scrollable by the user with scroll bars and working well at this point.  I would like to know if there is any way this could be modified so that the user could drag the panel with the cursor as well? If so, can you supply the correct event and code? 
Answers (4)
0
Alexandru Lazar

Alexandru Lazar

NA 62 0 14y

Sorry for the C# code; I hadn't noticed what forum this was on, but I'm sure you can translate all that in VB.NET
0
Rob Cro

Rob Cro

NA 3 2.6k 14y
Is this forum for C# or Visual Basic?
0
Sam Hobbs

Sam Hobbs

NA 28.7k 1.3m 14y
Be sure to also look at the recommended articles listed in the right side of the forum windows.
0
Alexandru Lazar

Alexandru Lazar

NA 62 0 14y
Here is a very ugly implementation (I don't have the time to make it performant), but you can get the idea of how it works.

private bool panelIsGrabbed = false;
private int xDistance, yDistance;

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
   xDistance = panel1.Location.X - e.X;
   yDistance = panel1.Location.Y - e.Y;
   panelIsGrabbed =
true;
}

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
   if (panelIsGrabbed)
   {
      panel1.Location =
new Point(e.X + xDistance, e.Y + yDistance);
   }
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
   panelIsGrabbed =
false;
}