C# 如何在窗体上移动picturebox,以便当它到达窗体的末尾时,它将循环回另一侧?

C# 如何在窗体上移动picturebox,以便当它到达窗体的末尾时,它将循环回另一侧?,c#,C#,我已经完成了从左到右的操作,在到达表单末尾后,它成功返回到开始位置以下是我的代码: pictureBox1.Left +=10; if (pictureBox1.Left >= this.Width ) { pictureBox1.Left = 0 - pictureBox1.Width; //Move the picturebox goes to start again ... } 现在我的问题是,如果PictureBox从右向左移动

我已经完成了从左到右的操作,在到达表单末尾后,它成功返回到开始位置以下是我的代码:

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
,但如何使其在开始位置重新开始?

您可以尝试另一个
循环:

像这样:

  // 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;
  }
//我们从pictureBox1.Left(初始位置)开始,步骤=10
//无条件(无限循环)
//向右移动
for(int left=pictureBox1.left,initial=pictureBox1.left,step=10;;left+=step){
//如果我们找到[0-步长..宽度+步长]范围,我们
//1.转到初始位置
//2.反向
如果(左>=宽度+步距和左<代码>如果(图片bx1.left