c# 计时器显示
时间: 2023-07-27 11:14:26 浏览: 67
在 C# 中使用计时器可以显示时间,可以使用 Timer 控件实现。以下是一个简单的示例代码:
```csharp
using System;
using System.Windows.Forms;
namespace TimerDemo
{
public partial class Form1 : Form
{
private Timer timer;
private int count;
public Form1()
{
InitializeComponent();
timer = new Timer();
timer.Interval = 1000; // 每隔1秒触发一次
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
count++;
label1.Text = TimeSpan.FromSeconds(count).ToString(); // 将秒数转换为时间格式显示在 label 控件上
}
}
}
```
在窗体的构造函数中创建一个 Timer 控件,并设置其间隔为 1 秒。在 Tick 事件中,每次计时器触发时增加计数变量 count 的值,然后将其转换为时间格式并显示在 label1 控件上。