Use Label and Timer to create a Clock : Label « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » GUI Windows Form » Label 




Use Label and Timer to create a Clock
 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form {
    public System.Timers.Timer timer1;
    private System.Windows.Forms.Label label1;
    public Form1() {
        this.timer1 = new System.Timers.Timer();
        this.label1 = new System.Windows.Forms.Label();
        ((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
        this.SuspendLayout();
        // 
        // timer1
        // 
        this.timer1.Enabled = true;
        this.timer1.SynchronizingObject = this;
        this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimerElapsed);
        // 
        // label1
        // 
        this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif"14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
        this.label1.ForeColor = System.Drawing.SystemColors.Highlight;
        this.label1.Location = new System.Drawing.Point(248);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(22448);
        this.label1.TabIndex = 0;
        this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(513);
        this.ClientSize = new System.Drawing.Size(29269);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.label1});
        this.Text = "My Clock";
        this.Load += new System.EventHandler(this.Form1_Load);
        ((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
        this.ResumeLayout(false);

    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }

    private void Form1_Load(object sender, System.EventArgs e) {
        timer1.Interval = 1000;
        timer1.Start();
        timer1.Enabled = true;
    }

    private void OnTimerElapsed(object sender, System.Timers.ElapsedEventArgs e) {
        label1.Text = DateTime.Now.ToString();
    }
}

 














Related examples in the same category
1.Label: Localtion, Name, Size, TabIndex, Text
2.Add image to Label and set ImageAlign to MiddleRightAdd image to Label and set ImageAlign to MiddleRight
3.Label grows as the inner textLabel grows as the inner text
4.Label auto resizeLabel auto resize
5.Set Label Background, foreground color and border styleSet Label Background, foreground color and border style
6.Marquee Label HostMarquee Label Host
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.