0
点赞
收藏
分享

微信扫一扫

104,c#定时器实现闪烁灯

_鱼与渔_ 2024-03-18 阅读 8

 //colorIndex计数

       private int colorIndex;

       //定义颜色数组

       private Color[] colors;




 //添加颜色

           colors = new Color[] { Color.Red, Color.Blue, Color.Cyan };

           //创建定时器

           Timer timer = new Timer();

           timer.Interval = 2000;

           timer.Tick += Timer_Tick;

           timer.Start();



  private void Timer_Tick(object sender, EventArgs e)

       {

          //给背景添加颜色,以colorIndex计数为索引

           BackColor = colors[colorIndex];

           //计数递增

           colorIndex++;

           //如果计数大于设定的颜色数量,则计数归0,实现循环

           if (colorIndex >= colors.Length)

           {

               colorIndex = 0;

           }

       }

举报

相关推荐

0 条评论