G
Guest
Can someone tell me how I can move an object, in this case a listbox, over a
form. The code below works, but not when the form is custom sized. It works
perfectly when the form is maximized. And what is the best way to give the
object boundries to move within?
Hope someone can help me.
TIA,
Arjan.
/*Begin Code*/
/*Source: MSDN*/
void ListBox1MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
int xOffset;
int yOffset;
if(e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight -
SystemInformation.FrameBorderSize.Height;
mouseOffset = new Point(xOffset, yOffset);
isMouseDown = true;
}
}
void ListBox1MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (isMouseDown)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
listBox1.Location = mousePos;
}
}
void ListBox1MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
isMouseDown = false;
}
/*End Code*/
form. The code below works, but not when the form is custom sized. It works
perfectly when the form is maximized. And what is the best way to give the
object boundries to move within?
Hope someone can help me.
TIA,
Arjan.
/*Begin Code*/
/*Source: MSDN*/
void ListBox1MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
int xOffset;
int yOffset;
if(e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight -
SystemInformation.FrameBorderSize.Height;
mouseOffset = new Point(xOffset, yOffset);
isMouseDown = true;
}
}
void ListBox1MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (isMouseDown)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
listBox1.Location = mousePos;
}
}
void ListBox1MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
isMouseDown = false;
}
/*End Code*/