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
,pictureBox1.Left -=10
但我怎样才能让它在它的起始位置重新开始呢?
解决方案
您可以尝试另一个for
循环:
像这样:
// 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;
}
推荐阅读
- c - scanf 跳过(尝试添加空格和\n)
- java - RecyclerView 单击并替换另一个活动中的图像
- javascript - 我在 webpack.DefinePlugin 中定义的变量未定义
- python - 使用 os 库在 python 中更改目录
- python - Tkinter Notebook 中的可滚动页面
- dom - 从 react-reason DOM ref 获取 clientHeight
- python - Python SQLite3如何将单列连接到多个表中
- python - OSError: [Errno 98] 地址已在使用中 在 Apache2 上运行 Flask 应用程序
- mysql - 使用 DATE_ADD 和 CURRENT_DATE 在表中查找今天发生的事情
- c++ - 如何修复未在范围内声明