Move button
-
Hi! I want to know how to move buttons inside a windows form. I tried in some ways, but nothing..:( People said me to use drag&drop property but I think it is not the right way.. Can someone give me a hand? If it is not to ask too much, could you write me the code? Thanks a lot!
-
Hi! I want to know how to move buttons inside a windows form. I tried in some ways, but nothing..:( People said me to use drag&drop property but I think it is not the right way.. Can someone give me a hand? If it is not to ask too much, could you write me the code? Thanks a lot!
-
I am still trying but I don't succeed in understanding how to use it.. HELP ME!!! I have to use SetBounds property inside MouseMove event? Could you write me, please, some line of code?
-
I am still trying but I don't succeed in understanding how to use it.. HELP ME!!! I have to use SetBounds property inside MouseMove event? Could you write me, please, some line of code?
Just as a starting point (you need to adjust this a bit): Create an empty form with a button called
button1
. Then add the following code:bool attached = false;
Point oldPos;private void button1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
attached = true;
oldPos = button1.PointToScreen(new Point(e.X, e.Y));
}private void button1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Move(button1.PointToScreen(new Point(e.X, e.Y)));
}private void button1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
attached = false;
}private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Move(PointToScreen(new Point(e.X, e.Y)));
}private void Move(Point p)
{
if (attached)
{
button1.Left -= oldPos.X - p.X;
button1.Top -= oldPos.Y - p.Y;
oldPos = p;
}
}Dont forget to attach the eventhandlers
-
Just as a starting point (you need to adjust this a bit): Create an empty form with a button called
button1
. Then add the following code:bool attached = false;
Point oldPos;private void button1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
attached = true;
oldPos = button1.PointToScreen(new Point(e.X, e.Y));
}private void button1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Move(button1.PointToScreen(new Point(e.X, e.Y)));
}private void button1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
attached = false;
}private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Move(PointToScreen(new Point(e.X, e.Y)));
}private void Move(Point p)
{
if (attached)
{
button1.Left -= oldPos.X - p.X;
button1.Top -= oldPos.Y - p.Y;
oldPos = p;
}
}Dont forget to attach the eventhandlers
This is a great solution! Thanks a lot, really. I have just tried it, and it works. Really good. Thanx, thanx and thanx!