有始以来最简单的Clock(C# Winform)
//Clockclass Clock : INotifyPropertyChanged
{
private Timer timer = new Timer();
private DateTime currentTime = DateTime.Now;
public Clock()
{
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
currentTime = DateTime.Now;
PropertyChangedEventArgs arg = new PropertyChangedEventArgs("CurrentTime");
OnPropertyChanged(arg);
}
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, e);
}
public DateTime CurrentTime
{
get { return currentTime; }
}
#region INotifyPropertyChanged 成员
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
//调用
Clock clock = new Clock();
label1.DataBindings.Add("Text", clock, "CurrentTime", true);
大家觉得如何?