I have a custom user control in my program. It is a panel which needs to be resizable from the left-hand side. Here is my code:
private void ResizePanel_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && e.X == ClientRectangle.Left)
{
resizeMode = true;
}
}
private void ResizePanel_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
resizeMode = false;
}
}
private void ResizePanel_MouseMove(object sender, MouseEventArgs e)
{
if (resizeMode == true)
{
Size newSize = new Size();
newSize.Height = Height;
newSize.Width = Math.Abs(e.X - ClientRectangle.Left); // Distance between the mouse position and
// left side of the panel
if (e.X < this.ClientRectangle.Left)
{
Width = newSize.Width;
Left -= newSize.Width;
}
}
}
In theory, the solution would be to move the panel to the left by the new width as the width increases. That's what this code is supposed to do. The problem with it at the moment is that as the panel moves to the left, the width stays the same and doesn't increase. Is there a way to do this so I can grab the control on the left side and drag to the left, so the size increases and it appears to stay in place?