天天看點

c# WinForm窗體實作動态時間展示

動态展示時間窗體

c# WinForm窗體實作動态時間展示

控件命名示例

c# WinForm窗體實作動态時間展示

元件Timer

啟動,機關1000毫秒

c# WinForm窗體實作動态時間展示

Timer事件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyProbject
{
    public partial class lblNum700 : Form
    {
        public lblNum700()
        {
            InitializeComponent();
            //調用方法直接顯示目前時間
            //消除延時
            currentTime_Tick(null,null);
        }
        //Timer計時器事件
        private void currentTime_Tick(object sender, EventArgs e)
        {
            this.lblYeak.Text = DateTime.Now.Year.ToString();
            this.lblMonth.Text = DateTime.Now.Month.ToString();
            this.lblDay.Text = DateTime.Now.Day.ToString();
            //this.lblTime.Text = DateTime.Now.ToShortTimeString();
            this.lblTime.Text = DateTime.Now.ToLongTimeString();
           // this.lblWeek.Text = DateTime.Now.DayOfWeek.ToString();
            switch(DateTime.Now.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    this.lblWeek.Text = "一";
                    break;
                case DayOfWeek.Tuesday:
                    this.lblWeek.Text = "二";
                    break;
                case DayOfWeek.Wednesday:
                    this.lblWeek.Text = "三";
                    break;
                case DayOfWeek.Thursday:
                    this.lblWeek.Text = "四";
                    break;
                case DayOfWeek.Friday:
                    this.lblWeek.Text = "五";
                    break;
                case DayOfWeek.Saturday:
                    this.lblWeek.Text = "六";
                    break;
                case DayOfWeek.Sunday:
                    this.lblWeek.Text = "日";
                    break;

            }
        }
    }
}
           
c# WinForm窗體實作動态時間展示

消除延時

啟動時即時調用目前時間

c# WinForm窗體實作動态時間展示