C#定时器出现错误的问题

ccbbcc 2024-10-22 13:15:23

代码:

using System;
using System.Windows.Forms;

public class TimerExampleForm : Form
{
    private Timer timer;
    private int counter = 0;
    private Label label;

    public TimerExampleForm()
    {
        // 初始化Label控件  
        label = new Label();
        label.Location = new System.Drawing.Point(50, 50);
        label.Size = new System.Drawing.Size(200, 30);
        this.Controls.Add(label);

        // 初始化Timer控件  
        timer = new Timer();
        timer.Interval = 1000; // 设置时间间隔为1000毫秒(1秒)  
        timer.Tick += new EventHandler(OnTimedEvent);
        timer.Start(); // 启动定时器  
    }

    private void OnTimedEvent(Object myObject, EventArgs myEventArgs)
    {
        // 在这里编写定时执行的代码  
        counter++;
        label.Text = "Counter: " + counter.ToString();
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new TimerExampleForm());
    }
}

 

Timer下面有波浪线,该如何解决?谢谢

...全文
91 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

111,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧