C#計時器 比如秒錶怎麼設計?
timer1的interval設定成10。
程式碼如下:
System。Diagnostics。Stopwatch sw ;
//開始按鈕
private void button1_Click(object sender, EventArgs e)
{
sw = new System。Diagnostics。Stopwatch();
sw。Start();
timer1。Start();
}
//停止按鈕
private void button2_Click(object sender, EventArgs e)
{
sw。Stop();
TimeSpan ts=sw。Elapsed ;
label1。Text = String。Format(“{0}天{1}小時{2}分{3}秒{4}毫秒”, ts。Days, ts。Hours, ts。Minutes, ts。Seconds,ts。Milliseconds );
}
private void timer1_Tick(object sender, EventArgs e)
{
TimeSpan ts = sw。Elapsed;
label1。Text = String。Format(“{0}天{1}小時{2}分{3}秒{4}毫秒”, ts。Days, ts。Hours, ts。Minutes, ts。Seconds, ts。Milliseconds);
}
using system; using system。collections。generic; using system。componentmodel; using system。data; using system。drawing; using system。linq; using system。text; using system。windows。forms; namespace windowsformsapplication1 { public partial class form1 : form { public form1() { initializecomponent(); } ///
/// //毫秒 ///
private int ms = 0; ///
/// 秒 ///
private int s = 0; ///
/// 分鐘 ///
private int m = 0; ///
/// 小時 ///
private int h = 0; ///
/// 開始計時按鈕 ///
///
///
private void button1_click(object sender, eventargs e) { //定時器有效,計時開始 timer1。enabled = true; } ///
/// 定時器 ///
///
///
private void timer1_tick(object sender, eventargs e) { if (ms < 99) { ms++; } else if(s<59) { ms = 0; s++; } else if (m < 59) { s = 0; m++; } else { h++; } label8。text = ms。tostring(); label7。text = s。tostring(); label6。text = m。tostring(); label5。text = h。tostring(); } ///
/// 結束計時按鈕 ///
///
///
private void button2_click(object sender, eventargs e) { timer1。enabled = false; } } }