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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#自定义控件滚动条的实现方法有多种。根据提供的引用内容,我可以为您提供以下方法和步骤来实现滚动条功能: 方法一: 1. 创建一个控件类,可以继承自Panel或者UserControl。该类将作为滚动条的容器。 2. 在该控件添加需要滚动的控件作为子控件。 3. 为该控件类添加滚动条控件,可以使用VScrollBar(竖向滚动条)或HScrollBar(横向滚动条)控件。 4. 设置滚动条控件的属性,如Maximum(可滚动范围的上限值)、SmallChange(小距离移动值)、LargeChange(大距离移动值)和Value(滚动条的当前位置)。 5. 给滚动条控件的Scroll事件添加处理方法,以便在滚动时对子控件进行相应操作。 方法二: 1. 创建一个视图类,为自定义控件动态添加滚动条,并处理滚动条的显示、隐藏和滚动事件。 2. 在该视图类的构造函数传入自定义控件实例。 3. 在视图类创建VScrollBar(竖向滚动条)和HScrollBar(横向滚动条)控件,并将其添加到自定义控件。 4. 设置滚动条控件的属性,如Dock(使滚动条固定在底部或右侧)、Maximum(可滚动范围的上限值)和LargeChange(大距离移动值)。 5. 订阅滚动条控件的事件,如Scroll(滚动条发生滚动时触发事件)和MouseWheel(根据滚轮滚动方向滚动滚动条)。 6. 在事件处理方法,根据滚动条的值和滚动方向,对自定义控件进行滚动操作。 以上是两种常见的实现滚动条功能的方法。您可以根据自己的需求选择其一种或根据具体情况进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值