Csharp/CSharp Tutorial/GUI Windows Forms/Timer

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Repaint control in Timer event

using System;
using System.Drawing;
using System.Windows.Forms;
public class Timers : Form
{
  Label lblTime;
  string strFormat;
  public Timers()
  {
    Size = new Size(300,100);
    strFormat = "dddd, MMMM d, yyyy  h:mm:ss tt";
    lblTime = new Label();
    lblTime.Parent = this;
    lblTime.Size = new Size((int)(ClientSize.Width * .8),25);
    lblTime.Location = new Point((int)(ClientSize.Width * .1), 
                (int)(ClientSize.Height * .4));
    lblTime.BorderStyle = BorderStyle.FixedSingle;
    lblTime.Text = DateTime.Now.ToString(strFormat);
    lblTime.TextAlign = ContentAlignment.MiddleCenter;
    Timer t = new Timer();
    t.Interval = 1000;    // 1 seconds
    t.Start();
    t.Tick += new EventHandler(t_Tick);
    
  } 
  static void Main() 
  {
    Application.Run(new Timers());
  }
  private void t_Tick(object sender, EventArgs e)
  {
    lblTime.Text = DateTime.Now.ToString(strFormat);
  }
}