c#.net 計時器

第一步,用WPF (.NET Framework),添加個TextBox,簡單設置屬性。this

第二步,貼代碼spa

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading; //添加Threading類

namespace ClockSample
{

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DispatcherTimer ClockOut = new DispatcherTimer(); // 類的實例化--設置對象(實例成員)
            ClockOut.Interval = TimeSpan.FromSeconds(1); //設置屬性
            ClockOut.Tick += ClockOut_Tick;// 連接事件處理器
            ClockOut.Start(); 
        }

        private void ClockOut_Tick(object sender, EventArgs e)
        {
            this.ClockBox.Text = DateTime.Now.ToString();
        }
    }
}

 

心得:初識類的使用方法。 對象

相關文章
相關標籤/搜索