Repaint control in Timer event : Timer « GUI Windows Forms « C# / CSharp Tutorial






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);
  }
}








23.75.Timer
23.75.1.Repaint control in Timer eventRepaint control in Timer event