制作一個簡單的日期時間的時鐘的UserControl....
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using My_Diarys;
namespace My_Diarys.UserControls
{
/// <summary>
/// Summary description for XSysDateTime.
/// </summary>
public class XSysDateTime : System.Windows.Forms.UserControl
{
private XUIControls.XLabel Lbl_SysDateTime;
private System.Windows.Forms.Timer Timer;
private System.ComponentModel.IContainer components;
public XSysDateTime()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitializeComponent call
}
/// <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 Component 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.Lbl_SysDateTime = new XUIControls.XLabel();
this.Timer = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// Lbl_SysDateTime
//
this.Lbl_SysDateTime.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.Lbl_SysDateTime.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.Lbl_SysDateTime.Location = new System.Drawing.Point(0, 0);
this.Lbl_SysDateTime.Name = "Lbl_SysDateTime";
this.Lbl_SysDateTime.Size = new System.Drawing.Size(170, 24);
this.Lbl_SysDateTime.TabIndex = 0;
this.Lbl_SysDateTime.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// Timer
//
this.Timer.Tick += new System.EventHandler(this.Timer_Tick);
//
// XSysDateTime
//
this.Controls.Add(this.Lbl_SysDateTime);
this.Name = "XSysDateTime";
this.Size = new System.Drawing.Size(172, 28);
this.Load += new System.EventHandler(this.XSysDateTime_Load);
this.ResumeLayout(false);
}
#endregion
private void XSysDateTime_Load(object sender, System.EventArgs e)
{
this.Timer.Start(); /// 記時器開時運行
//SysDateTime();
}
// private void SysDateTime()
// {
// this.Timer.Interval = 1000;
// this.Lbl_SysDateTime.Text = DateTime.Now.ToString();
// }
private void Timer_Tick(object sender, System.EventArgs e)
{
this.Timer.Interval = 1000; //記時以秒為單位
this.Lbl_SysDateTime.Text = DateTime.Now.ToString("yyyy年MM月yy日 HH:mm:ss");
}
}
}
效果圖: