[英]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.