简体   繁体   English

我想使用计时器在winform中每1秒更改一次背景色

[英]I want to change backcolor every 1 second in winform using timer

I want to change backgroundcolor every 1 second using timer in winform. 我想使用winform中的计时器每1秒更改一次背景颜色。 For example Red -> SystemColors.Control -> red -> SystemColors.Control ->.. 例如红色-> SystemColors.Control->红色-> SystemColors.Control-> ..

I have menuitem in mainUI. 我在mainUI中有menuitem。 So when I click this control, I want to see automatically changing of mainUI's background color. 因此,当我单击此控件时,我想查看mainUI背景颜色的自动更改。

Here is code what I wrote. 这是我写的代码。 what should I change in this code? 我该更改什么代码?

private void RadMenuItem9_Click(object sender, EventArgs e)
{
    System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
    timer.Enabled = true;
    timer.Interval = 2000;
    timer.Tick += new EventHandler(Timer1_Tick);
    timer.Start();
}

private void Timer1_Tick(object sender, EventArgs e)
{
    BackColor = Color.Red;
    Thread.Sleep(1000);
    BackColor = SystemColors.Control;
}

You need to make logic for switching colors. 您需要为切换颜色制定逻辑。 Here since you want to change between two colors you can check the current color and switch to the other. 在这里,由于要在两种颜色之间切换,因此可以检查当前颜色并切换到另一种颜色。 You can't use Sleep since that will block the UI thread and no changes will be shown on screen while the thread is blocked. 您不能使用Sleep因为这将阻止UI线程,并且在线程被阻止时不会在屏幕上显示任何更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM