Friday, 6 September 2013

Simple Button Move

Simple Button Move

I have a card game application and i want to create a simple animation
that will make the button move when it is clicked and dragged.
I have tried.
bool _Down = false;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
_Down = true;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
_Down = false;
button1.Location = e.Location;
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (_Down)
{
button1.Location = e.Location;
}
}
This doesn't work also. The effect i get is that when the button is
clicked and dragged. The button is not visible until the mouse is
released, and also, the button doesn't actually stay at the location of
the mouse.
I also tried
bool _Down = false;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
_Down = true;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
_Down = false;
button1.Location = Cursor.Position;
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (_Down)
{
button1.Location = Cursor.Position;
}
}
This works better than the first one as the button is visible when dragged
and stops at mouse position but the only problem is that Cursor.Position
returns the cursor position in relativeness to the screen not the form
therefore, the button doesn't actually move at the pace of the cursor.
Pls what can i do to achieve what i want?

No comments:

Post a Comment