I have a winforms app. Inside, I have one panel (panel1), and inside this panel, another panel (panel2) with buttons inside.
I want to move panel2 horizontally inside panel1 when I mousedown in some button.
I’ve made this in each button inside panel2.
this.button4.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btMouseDown);
this.button4.MouseMove += new System.Windows.Forms.MouseEventHandler(this.btMouseMove);
this.button4.MouseUp += new System.Windows.Forms.MouseEventHandler(this.btMouseUp);
and
void btMouseMove(object sender, MouseEventArgs e)
{
if (_mouseDown)
panel2.Location = PointToClient(this.panel2.PointToScreen(new Point(e.X - _mousePos.X, e.Y - _mousePos.Y)));
}
void btMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_mouseDown = true;
_mousePos = new Point(e.X, e.Y);
}
}
void btMouseUp(object sender, MouseEventArgs e)
{
if (_mouseDown)
{
_mouseDown = false;
}
}
This code moves the panel2 correctly inside the panel1, but I want to move the panel only horizontally, and this code moves to mouse location. I tried to put
Point(e.X - _mousePos.X, 3)
Instead of
Point(e.X - _mousePos.X, e.Y - _mousePos.Y)
But panel2 disappears. I would like to know how to move the panel2 inside the panel1 only horizontally.
Thanks a lot.
1 Answer