我已经完成了从左到右的操作,到达表单结尾后,它成功返回到起始位置,这是我的代码:
pictureBox1.Left +=10;
if (pictureBox1.Left >= this.Width )
{
pictureBox1.Left = 0 - pictureBox1.Width; //Move the picturebox goes to start again
...
}
现在我的问题是,如果PictureBox从右向左移动,该怎么办?我知道我将只将pictureBox1.Left +=10
更改为pictureBox1.Left -=10
的代码,但是如何使它从起始位置重新开始?
答案 0 :(得分:0)
您可以尝试另一个for
循环:
How to loop picturebox going left inifnity
赞:
// we start at pictureBox1.Left (which is initial position) with step = 10
// no condition (infinite loop)
// move to the right
for (int left = pictureBox1.Left, initial = pictureBox1.Left, step = 10; ; left += step) {
// If we beoynd [0 - step..Width + step] range, we
// 1. Go to initial position
// 2. Reverse direction
if (left >= Width + step && left <= -step) {
left = initial;
step = -step;
}
pictureBox1.Left = left;
}