The clock control : Custom Control « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » Custom Control 
23.56.3.The clock control
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;


public class DigitalClock : System.Windows.Forms.UserControl {
    private System.Windows.Forms.Timer timer1;
    private System.ComponentModel.IContainer components;

    public DigitalClock() {
        this.components = new System.ComponentModel.Container();
        this.timer1 = new System.Windows.Forms.Timer(this.components);
        this.timer1.Enabled = true;
        this.timer1.Interval = 1000;
        this.timer1.Tick += new System.EventHandler(this.OnTick);
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);
    }
    private void OnPaint(object sender,  System.Windows.Forms.PaintEventArgs e) {
        System.DateTime dt = System.DateTime.Now;
        String Text = dt.ToLongTimeString();
        e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor),ClientRectangle);
    }
    private void OnTick(object sender, System.EventArgs e) {
        this.Refresh();
    }
}
public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.Label label1;

    private DigitalClock clock= new DigitalClock();

    public Form1() {
        this.label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        this.label1.Location = new System.Drawing.Point(1624);
        this.label1.Size = new System.Drawing.Size(12816);
        this.label1.Text = "Clock:";

        this.clock.Location = new System.Drawing.Point(1650);
        this.clock.Size = new System.Drawing.Size(12880);
        this.AutoScaleBaseSize = new System.Drawing.Size(513);
        this.ClientSize = new System.Drawing.Size(292273);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
              this.label1,
              this.clock});
        this.ResumeLayout(false);
    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
}
23.56.Custom Control
23.56.1.Create your own component by subclassing System.ComponentModel.Component
23.56.2.Create controlCreate control
23.56.3.The clock control
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.