J
Jay Dee Thompson
User resizable control.
In order to make a control that the user can resize by dragging the
edges of the control I have added MouseDown, MouseUp and MouseMove
events that allow the user to alter the size of the control.
This is a small example of what I mean allowing the user to drag the
right edge of the control.
<code>
private bool reSizeRight = false;
private Point lastMousePosition;
private void control_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (e.X > this.Width - this.Margin.Right)
{
this.reSizeRight = true;
}
}
}
private void control_MouseUp(object sender, MouseEventArgs e)
{
this.reSizeRight = false;
}
private void control_MouseMove(object sender, MouseEventArgs e)
{
if (this.reSizeRight)
{
int offset = e.X - this.lastMousePosition.X;
if (offset != 0)
{
this.Width += offset;
}
}
this.lastMousePosition = e.Location;
}
</code>
Then my next task was to be able to resize the control when it was
docked to either the right or the left side of its parent.
Dock.Left was simple by just adding an “If” statement to the code
above checking the controls dock property was “left”.
My problem.
If the control is docked to the right and I increase/decrease the
width of the control then the right edge of the control stays in the
same place and the left edge of the control will move accordingly.
This would all be good but the MouseMove event is repeatedly re-
occurring in a loop.
I think that it is because the control is being re positioned by the
MouseMove method yet the mouse is staying in the same place therefore
the mouse in relation to the control has changed, so the MouseMove
event is being re triggered.
I hope that makes sense I carnet think of a good way to explain.
I have tried un-hooking the method from the MouseMove event before
resizing the width then re-hooking the method back to the event call
but it had no effect.
Please ask if someone thinks I should post more code to help explain
the issue, I didn’t want to make the post any bigger.
In order to make a control that the user can resize by dragging the
edges of the control I have added MouseDown, MouseUp and MouseMove
events that allow the user to alter the size of the control.
This is a small example of what I mean allowing the user to drag the
right edge of the control.
<code>
private bool reSizeRight = false;
private Point lastMousePosition;
private void control_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (e.X > this.Width - this.Margin.Right)
{
this.reSizeRight = true;
}
}
}
private void control_MouseUp(object sender, MouseEventArgs e)
{
this.reSizeRight = false;
}
private void control_MouseMove(object sender, MouseEventArgs e)
{
if (this.reSizeRight)
{
int offset = e.X - this.lastMousePosition.X;
if (offset != 0)
{
this.Width += offset;
}
}
this.lastMousePosition = e.Location;
}
</code>
Then my next task was to be able to resize the control when it was
docked to either the right or the left side of its parent.
Dock.Left was simple by just adding an “If” statement to the code
above checking the controls dock property was “left”.
My problem.
If the control is docked to the right and I increase/decrease the
width of the control then the right edge of the control stays in the
same place and the left edge of the control will move accordingly.
This would all be good but the MouseMove event is repeatedly re-
occurring in a loop.
I think that it is because the control is being re positioned by the
MouseMove method yet the mouse is staying in the same place therefore
the mouse in relation to the control has changed, so the MouseMove
event is being re triggered.
I hope that makes sense I carnet think of a good way to explain.
I have tried un-hooking the method from the MouseMove event before
resizing the width then re-hooking the method back to the event call
but it had no effect.
Please ask if someone thinks I should post more code to help explain
the issue, I didn’t want to make the post any bigger.