C#自定义控件实例

本文介绍如何在VS2010中创建一个名为TimeLabel的复合控件,该控件利用Label和Timer控件实时显示系统时间,并支持多种时间格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文将写一个简单的复合控件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

转载于:https://www.cnblogs.com/Mageric/archive/2012/07/31/2617200.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值