C# 視窗程式範例 -- 數位時鐘

專案下載:Clock.zip

介面設計

請從工具箱中拉出一個 label 與一個 Timer,如下圖所示。

ClockDesign.png

程式內容

然後將 Form1.cs 的程式改成如下內容。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TimerDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            timer1_Tick(this, null);
            timer1.Interval = 1000; // 設定每秒觸發一次
            timer1.Enabled = true; // 啟動 Timer
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime time = DateTime.Now;
//            label1.Text = time.TimeOfDay.ToString();
//            label1.Text = time.Hour + ":" + time.Minute + ":" + time.Second;
            label1.Text = String.Format("{0:00}:{1:00}:{2:00}", 
                                                   time.Hour, time.Minute, time.Second);
        }
    }
}
	

執行結果

這樣就設計好一個數位時鐘了。

ClockRun.png

文章來源:陳鍾誠 (2010年09月28日),(網頁標題) C# 視窗程式範例 — 數位時鐘,(網站標題) 免費電子書:C# 程式設計,2010年09月28日,取自 http://cs0.wikidot.com/clock ,網頁修改第 5 版。


arrow
arrow

    Johnson峰 發表在 痞客邦 留言(0) 人氣()