C#-在winform文本框中显示运行时间



加载表单时,如何在文本框中自动显示当前运行时间或每秒增量?

我试过

myTextBox.Text = DateTime.Now.ToString();

但仅显示静态时间。

试试这个

public partial class FormWithTimer : Form
{
    Timer timer = new Timer();
    public FormWithTimer()
    {
        InitializeComponent();
        timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
        timer.Interval = 1000;              // Timer will tick evert second
        timer.Enabled = true;                       // Enable the timer
        timer.Start();                              // Start the timer
    }
    void timer_Tick(object sender, EventArgs e)
    {
        myTextBox.Text = DateTime.Now - Process.GetCurrentProcess().StartTime;
    }
}

但是,在调试时,这可能会显示vshost进程时间。

添加一个计时器,将间隔设置为1并确保其已启用,然后在计时器的事件上放入代码

最新更新