我有一个Windows窗体应用程序,带有一个包含图像的PictureBox控件。我想缓慢移动PictureBox控件到右边。这是我的代码:
Point currentPoint = pictureBox_Logo.Location;
for (int i = 0; i < 25; i++)
{
pictureBox_Logo.Location = new Point(pictureBox_Logo.Location.X + 1, pictureBox_Logo.Location.Y);
Thread.Sleep(30);
}
这里的问题是,当执行代码而不是看到图片移动时,我看到了白色图片移动,并且移动停止,直到出现图片为止。
我缺少什么,我该怎么办?
代码:
public partial class Form1 : Form
{
void timer_Tick(object sender, EventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
pictureBox1.Location = new Point(x+25, y);
if (x > this.Width)
timer1.Stop();
}
public Form1()
{
InitializeComponent();
timer1.Interval = 10;
timer1.Tick += new EventHandler(timer_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Show();
timer1.Start();
}
}
原始线程在这里Move images in C#
尝试在Thread.Sleep(30)之后使用pictureBox_Logo.Refresh();或寻找标准的计时器控件。
我的代码写得很好,但是我做错了把代码放在事件中:
private void Form1_Shown(object sender, EventArgs e);
但是当我将代码放入按钮时,它可以正常工作。