GUI and timer

image_pdfimage_print


   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Clock
{
  /// <summary>
  /// Summary description for ClockForm.
  /// </summary>
  public class ClockForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Timer timer1;
    private System.Windows.Forms.Label label2;
    private System.ComponentModel.IContainer components;

    #region ClockForm Constructor
    public ClockForm()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
      // The following line sets the text for the label box.
      // To display no text, simply use two quote marks.
      label1.Text = "";
      // The following two statements set the text size and
      // cause the time to appear centered in the label box
      label1.Font = new System.Drawing.Font
        ("Microsoft Sans Serif", 24);
      label1.TextAlign = ContentAlignment.MiddleCenter;
      label2.Font = label1.Font;
      label2.TextAlign = ContentAlignment.MiddleCenter;
      // This.Text is the text that will appear in the form&#039;s
      // title bar.
//      this.Text = "My Clock";
      // The following three lines set the timer to tick
      // every second (1000 milliseconds), start the timer and
      // write the initial time to the label box
      timer1.Interval = 1000;
      timer1.Start ();
      SetClock ();
    }
    #endregion
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.components = new System.ComponentModel.Container();
      this.timer1 = new System.Windows.Forms.Timer(this.components);
      this.label1 = new System.Windows.Forms.Label();
      this.label2 = new System.Windows.Forms.Label();
      this.SuspendLayout();
      // 
      // timer1
      // 
      this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
      // 
      // label1
      // 
      this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.label1.Location = new System.Drawing.Point(24, 56);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(248, 48);
      this.label1.TabIndex = 0;
      this.label1.Text = "label1";
      // 
      // label2
      // 
      this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.label2.Location = new System.Drawing.Point(24, 136);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(248, 48);
      this.label2.TabIndex = 1;
      this.label2.Text = "label2";
      // 
      // ClockForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
      this.ClientSize = new System.Drawing.Size(292, 272);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.label2,
                                      this.label1});
      this.Name = "ClockForm";
      this.Text = "ClockForm";
      this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new ClockForm());
    }

    private void timer1_Tick(object sender, System.EventArgs e)
    {
      SetClock ();
    }
    protected void SetClock ()
    {
      string str = DateTime.Now.ToString();
      int index = str.IndexOf (" ");
      label1.Text = str.Substring (index + 1);
      label2.Text = str.Substring (0, index);
    }
  }
}