[英]C# - Display running time in winform textbox
加载表单时,如何在文本框中自动显示当前运行时间或每秒递增?
我试过了
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并确保已启用它,然后在计时器的事件中放入您的代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.