【发布时间】:2016-07-24 10:58:46
【问题描述】:
当用户单击按钮时,我正在尝试在预设的时间内更改 pictureBox 的背景色。我尝试使用计时器,但我在另一个问题上看到了这个Stopwatch。问题是循环内的代码运行不正常并且不断崩溃。我怎样才能使它工作?代码如下
private void b_click(object sender, EventArgs e)
{
Button button = sender as Button;
Dictionary <Button, PictureBox> buttonDict= new Dictionary<Button, PictureBox>();
//4 buttons
buttonDict.Add(bRED, pbRED);
buttonDict.Add(bBlue, pbBLUE);
buttonDict.Add(bGREEN, pbGREEN);
buttonDict.Add(bYELLOW, pbYELLOW);
Stopwatch s = new Stopwatch();
s.Start();
while (s.Elapsed < TimeSpan.FromSeconds(0.5))
{
buttonDict[button].BackColor = Color.Black;
label1.Text = "black";//This part does run
}
buttonDict[button].BackColor = Color.White; //the pictureBox does turn white
s.Stop();
}
【问题讨论】:
-
使用定时器。秒表仅用于测量开始和停止之间的时间(例如)。
标签: c# button click picturebox backcolor