C#自定义控件实例

本文将写一个简单的复合控件TimeLabel

1.打开VS2010,依次点击文件-新建-项目,如图:

image

选择Windows窗体控件库

在解决方案里可以修改下.cs文件名

image

当然也可以使用默认的

2.点击确定之后

image

设计窗口会出现一个150*150的容器

从工具箱里面拖一个Label控件到容器上

在拖一个Timer控件过来,将timer1的Interval属性设置为1000毫秒

image

PS:简单的说一下Timer控件,Timer控件也就是计时器控件,有两个重要的属性Interval和Tick事件,当Timer控件的Enable的属性设置为True时,计时器启动每经过Interval设定的时间间隔后就会执行Tick事件,直到其Enabled属性为false。

3.双击timer1控件,转至代码设计界面

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NumTextBox
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
            this.timer1.Enabled = true;//将计时器的enabled属性设置为true使计时器一开始就处于可用状态
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.label1.Text = DateTime.Now.ToString();//将系统时间转换成String类型添加到label的Text属性上
        }
    }
}

4.F5启动调试

image

一个简单的复合控件就完成了

5.关闭测试容器,切换到设计界面,选中容器,将容器的Size设置为160,12

选中label1将属性设置为:

imageimage

6.为了能适用不同的时间格式,可以为控件设置属性并公开。

切换到代码设计界面,修改一下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TimeLabel
{
    public partial class TimeLabel : UserControl
    {
        public enum timeStyle   //枚举
        {
            String,
            LongDateString,
            LongTimeString,
            ShortDateString,
            ShortTimeString,  
            自定义,
        }
        private timeStyle type;

        public timeStyle Type
        {
            get { return type; }
            set { type = value; }
        }


        public TimeLabel()
        {
            InitializeComponent();
            this.timer1.Enabled = true;  
        }

        protected void timer1_Tick(object sender, EventArgs e)
        {//根据选择调整显示
            switch (type)
            { 
                case timeStyle.LongDateString:
                    this.label1.Text = DateTime.Now.ToLongDateString();
                    break;
                case timeStyle.LongTimeString:
                    this.label1.Text = DateTime.Now.ToLongTimeString();
                    break;
                case timeStyle.ShortDateString:
                    this.label1.Text = DateTime.Now.ToShortDateString();
                    break;
                case timeStyle.ShortTimeString:
                    this.label1.Text = DateTime.Now.ToShortTimeString();
                    break;
                case timeStyle.String:
                    this.label1.Text = DateTime.Now.ToString();
                    break;
                default:
                    this.label1.Text = DateTime.Now.ToString("yyyy年MM月dd日 hh:mm:ss");
                    break;
            }
        }
     }
}

重新启动调试

运行效果如下:

image

posted @ 2012-07-31 17:54  Mageric  阅读(1076)  评论(0编辑  收藏  举报