iT邦幫忙

DAY 1
2

C# 程式學習系列 第 1

C# 小時鐘

c#

用 C# 開發小時鐘,使用了 Timer、Label
這是個小範例,是使用 C# 寫個小時鐘,使用了 Timer 與 Label,先將這兩個控制項加入,而 label 是用來顯示時間,Timer 是用來定時重複更新時間

程式碼如下

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 WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = 100;
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = DateTime.Now.ToString();
        }
    }
}

結果如下


下一篇
C# 判斷檔案是否存在
系列文
C# 程式學習30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
歪歪
iT邦新手 3 級 ‧ 2018-02-22 11:57:18

請問若Label1.text要顯示的日期格式想自訂
該怎麼寫

可以將DateTime.Now做格式化再依需求設定Label

我要留言

立即登入留言