Digital Clock with Date : Timer « Development Class « C# / C Sharp

Digital Clock with Date


using System;
using System.Drawing;
using System.Windows.Forms;
class DigitalClock: Form
     public static void Main()
          Application.Run(new DigitalClock());
     public DigitalClock()
          ResizeRedraw = true;
          Timer timer    = new Timer();
          timer.Tick    += new EventHandler(TimerOnTick);
          timer.Interval = 1000;
     private void TimerOnTick(object obj, EventArgs ea)
     protected override void OnPaint(PaintEventArgs pea)
          Graphics grfx    = pea.Graphics;
          DateTime dt      = DateTime.Now;
          string   strTime = dt.ToString("d") + "\n" + dt.ToString("T");
          SizeF    sizef   = grfx.MeasureString(strTime, Font);
          float    fScale  = Math.Min(ClientSize.Width  / sizef.Width,
                                      ClientSize.Height / sizef.Height);
          Font     font    = new Font(Font.FontFamily,
                                      fScale * Font.SizeInPoints);
          StringFormat strfmt = new StringFormat();
          strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center;
          grfx.DrawString(strTime, font, new SolidBrush(ForeColor), 
                          ClientRectangle, strfmt);


Related examples in the same category

1.Using TimerCallback
2.illustrates the use of the Timer classillustrates the use of the Timer class
3.Demonstrates using the System.Threading.Timer objectDemonstrates using the System.Threading.Timer object
4.Demonstrates using the System.Timers.Timer class 2Demonstrates using the System.Timers.Timer class 2
5.Control ModifierControl Modifier
6.Simple Clock
7.Interval, Tick, Stop
9.Test Timer