【发布时间】:2020-04-04 20:52:55
【问题描述】:
我正在尝试让我的图片框在屏幕上移动,但出现此错误:计时器内的当前上下文中不存在“图片”。我能做什么?
private void Button1_Click(object sender, EventArgs e)
{
var picture = new PictureBox
{
Name = "pictureBox",
Size = new Size(20, 20),
Location = new System.Drawing.Point(x, y),
Image = image1,
};
this.Controls.Add(picture);
timer1.Enabled = true;
}
private void Timer1_Tick(object sender, EventArgs e)
{
//redefine pictureBox position.
x = x - 50;
picture.Location = new System.Drawing.Point(x, y); //'picture' does not exist in the current context
}
【问题讨论】:
-
picture是一个局部变量,把它变成一个field -
如果您的问题已得到解答,请记得接受答案
标签: c# winforms button timer picturebox