使用 Button-Paint-Event 按钮不会在 for 循环的每次迭代中更新。但是每次迭代都会平滑地更新表单或面板。
因此,在执行此代码时,Button 以默认颜色开始,然后在 for 循环完成后,显示颜色数组中的最后一种颜色。它不会随着每次迭代而更新。
我的问题:有人知道,为什么每次迭代都没有更新 Button,但其他控件使用相同的代码?
void Main()
{
Color[] colors = new Color[10]
{
Color.White, Color.Red, Color.Blue, Color.Green, Color.Black,
Color.Purple, Color.Brown, Color.Yellow, Color.Gray, Color.Lime
};
Button button = new Button();
button.FlatStyle = FlatStyle.Flat;
button.Paint += (sender, e) =>
{
for (int i = 0; i < 10; i++)
{
e.Graphics.FillRectangle(
new SolidBrush(colors[i]),
new RectangleF(0, 0, button.Width, button.Height));
Thread.Sleep(100);
Application.DoEvents();
}
};
Form form = new Form();
form.Controls.Add(button);
form.Show();
}