C# 组合控件

打算做一各显示: 流量 值 单位这样一个控件黑色背景,绿色的字体

创建项目:

 

拖入五个label控件。设置Dock属性 前4个为Left,最后一个为Fill如下图。修改Name属性1-5分别为:lblTitle、lblSeparatorA、lblValue、lblSeparatorB、lblUnit

 

最后的效果图:

调用过程:工具箱->选择项->浏览->找到你编译的Dll文件->确定 之后像调用TextBox等控件一样,直接拖入窗体

 

具体过程看代码

namespace CustomControlLib
{
    partial class LabelFive
    {
        /// <summary> 
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码

        /// <summary> 
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.lblTitle = new System.Windows.Forms.Label();
            this.lblSeparatorA = new System.Windows.Forms.Label();
            this.lblValue = new System.Windows.Forms.Label();
            this.lblSeparatorB = new System.Windows.Forms.Label();
            this.lblUnit = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // lblTitle
            // 
            this.lblTitle.AutoSize = true;
            this.lblTitle.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.lblTitle.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lblTitle.Dock = System.Windows.Forms.DockStyle.Left;
            this.lblTitle.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lblTitle.ForeColor = System.Drawing.Color.Lime;
            this.lblTitle.Location = new System.Drawing.Point(0, 0);
            this.lblTitle.Name = "lblTitle";
            this.lblTitle.Size = new System.Drawing.Size(44, 16);
            this.lblTitle.TabIndex = 0;
            this.lblTitle.Text = "title";
            // 
            // lblSeparatorA
            // 
            this.lblSeparatorA.Dock = System.Windows.Forms.DockStyle.Left;
            this.lblSeparatorA.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lblSeparatorA.Location = new System.Drawing.Point(44, 0);
            this.lblSeparatorA.Name = "lblSeparatorA";
            this.lblSeparatorA.Size = new System.Drawing.Size(2, 15);
            this.lblSeparatorA.TabIndex = 1;
            // 
            // lblValue
            // 
            this.lblValue.AutoSize = true;
            this.lblValue.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.lblValue.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lblValue.Dock = System.Windows.Forms.DockStyle.Left;
            this.lblValue.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lblValue.ForeColor = System.Drawing.Color.Lime;
            this.lblValue.Location = new System.Drawing.Point(46, 0);
            this.lblValue.Name = "lblValue";
            this.lblValue.Size = new System.Drawing.Size(44, 16);
            this.lblValue.TabIndex = 2;
            this.lblValue.Text = "value";
            this.lblValue.TextChanged += new System.EventHandler(this.txtValue_TextChanged);
            // 
            // lblSeparatorB
            // 
            this.lblSeparatorB.Dock = System.Windows.Forms.DockStyle.Left;
            this.lblSeparatorB.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lblSeparatorB.Location = new System.Drawing.Point(90, 0);
            this.lblSeparatorB.Name = "lblSeparatorB";
            this.lblSeparatorB.Size = new System.Drawing.Size(2, 15);
            this.lblSeparatorB.TabIndex = 3;
            // 
            // lblUnit
            // 
            this.lblUnit.AutoSize = true;
            this.lblUnit.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.lblUnit.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lblUnit.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lblUnit.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lblUnit.ForeColor = System.Drawing.Color.Lime;
            this.lblUnit.Location = new System.Drawing.Point(92, 0);
            this.lblUnit.Name = "lblUnit";
            this.lblUnit.Size = new System.Drawing.Size(37, 16);
            this.lblUnit.TabIndex = 4;
            this.lblUnit.Text = "unit";
            // 
            // LabelFive
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.lblUnit);
            this.Controls.Add(this.lblSeparatorB);
            this.Controls.Add(this.lblValue);
            this.Controls.Add(this.lblSeparatorA);
            this.Controls.Add(this.lblTitle);
            this.Name = "LabelFive";
            this.Size = new System.Drawing.Size(158, 15);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label lblTitle;
        private System.Windows.Forms.Label lblSeparatorA;
        private System.Windows.Forms.Label lblValue;
        private System.Windows.Forms.Label lblSeparatorB;
        private System.Windows.Forms.Label lblUnit;
    }
}

代码窗口中的内容

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 CustomControlLib
{

    public partial class LabelFive : UserControl
    {
        /// <summary>
        /// 文本改变事件委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void ValueTextChangedHandler(object sender, System.EventArgs e);
        /// <summary>
        /// 文本改变事件
        /// </summary>
        public event ValueTextChangedHandler OnValueTextChanged; 

        /// <summary>
        /// 构造函数
        /// </summary>
        public LabelFive()
        {
            InitializeComponent();

            //给自定义属性赋值
            this.LabelBackColor = Color.Black;
            this.LabelUnitBackColor = Color.Black;
            this.LabelForeColor = Color.Lime;
            this.LabelUnitForeColor = Color.Lime;
            this.LabelFont = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
        }

        /// <summary>
        /// 名称
        /// 特性Category("Apearance")----作用:在控件的属性框里分类
        /// 特性Description("设置或获取title标签的文本")-----作用:单击该属性后,在属性对话框最下方显示的提示
        /// </summary>
        [Category("Apearance"), Description("设置或获取title标签的文本")]
        public string Text_Title
        {
            get { return lblTitle.Text; }
            set 
            { 
                lblTitle.Text = value;
                this.Width = SetWidth(); 
            }
        }
        /// <summary>
        /// 设置控件宽度
        /// </summary>
        /// <returns></returns>
        private int SetWidth()
        {
            return this.lblTitle.Width + this.lblSeparatorA.Width + this.lblValue.Width + this.lblSeparatorB.Width + this.lblUnit.Width + 2;
        }
        /// <summary>
        /// 数值
        /// </summary>
        [Category("Apearance"), Description("设置值")]
        public string Text_Value
        {
            get { return lblValue.Text; }
            set 
            { 
                lblValue.Text = value;
                this.Width = SetWidth();
            }
        }
        /// <summary>
        /// 单位
        /// </summary>
        [Category("Apearance"), Description("设置单位")]
        public string Text_Unit
        {
            get { return lblUnit.Text; }
            set 
            { 
                lblUnit.Text = value;
                this.Width = SetWidth();
            }
        }

        /// <summary>
        /// 标签前景色
        /// </summary>
        [Category("Apearance"), Description("设置或获取title、value的前景色")]
        public Color LabelForeColor
        {
            get { return lblTitle.ForeColor; }
            set 
            {
                lblTitle.ForeColor = value;
                lblValue.ForeColor = value;
            }
        }
        /// <summary>
        /// 标签背景色
        /// </summary>
        [Category("Apearance"), Description("设置或获取title、value的背景色")]
        public Color LabelBackColor
        {
            get { return lblTitle.BackColor; }
            set 
            {
                lblTitle.BackColor = value;
                lblValue.BackColor = value;
            }
        }

        /// <summary>
        /// 获取或设置用于显示单位的标签背景色
        /// </summary>
        [Category("Apearance"), Description("设置或获取unit的背景色")]
        public Color LabelUnitBackColor
        {
            get { return lblUnit.BackColor; }
            set
            {
                lblUnit.BackColor = value;
            }
        }

        /// <summary>
        /// 获取或设置用于显示单位的标签前景色
        /// </summary>
        [Category("Apearance"), Description("设置或获取unit的前景色")]
        public Color LabelUnitForeColor
        {
            get { return lblUnit.ForeColor; }
            set
            {
                lblUnit.ForeColor = value;
            }
        }

        /// <summary>
        /// 设置5个label的Font
        /// </summary>
        [Category("Apearance"), Description("设置或获取所有标签字体")]
        public Font LabelFont
        {
            get { return lblTitle.Font; }
            set
            {
                lblTitle.Font = value;
                lblSeparatorA.Font = value;
                lblValue.Font = value;
                lblSeparatorB.Font = value;
                lblUnit.Font = value;
                this.Height = lblTitle.Height + 1;
                this.Width = this.SetWidth();
            }
        }
        /// <summary>
        /// 文字改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void  txtValue_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (OnValueTextChanged != null)
                {
                    OnValueTextChanged(sender, e);
                }
            }
            catch(Exception Err) 
            {
                MessageBox.Show("LabelFive控件->OnValueTextChanged事件错误。\n" + Err.Message);
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/zhou8744/p/8410943.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值