打算做一各显示: 流量 值 单位这样一个控件黑色背景,绿色的字体
创建项目:
拖入五个label控件。设置Dock属性 前4个为Left,最后一个为Fill如下图。修改Name属性1-5分别为:lblTitle、lblSeparatorA、lblValue、lblSeparatorB、lblUnit
最后的效果图:
调用过程:工具箱->选择项->浏览->找到你编译的Dll文件->确定 之后像调用TextBox等控件一样,直接拖入窗体
具体过程看代码
namespace CustomControlLib{ partial class LabelFive { ////// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; ////// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region 组件设计器生成的代码 ////// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// 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 { ////// 文本改变事件委托 /// /// /// public delegate void ValueTextChangedHandler(object sender, System.EventArgs e); ////// 文本改变事件 /// public event ValueTextChangedHandler OnValueTextChanged; ////// 构造函数 /// 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))); } ////// 名称 /// 特性Category("Apearance")----作用:在控件的属性框里分类 /// 特性Description("设置或获取title标签的文本")-----作用:单击该属性后,在属性对话框最下方显示的提示 /// [Category("Apearance"), Description("设置或获取title标签的文本")] public string Text_Title { get { return lblTitle.Text; } set { lblTitle.Text = value; this.Width = SetWidth(); } } ////// 设置控件宽度 /// ///private int SetWidth() { return this.lblTitle.Width + this.lblSeparatorA.Width + this.lblValue.Width + this.lblSeparatorB.Width + this.lblUnit.Width + 2; } /// /// 数值 /// [Category("Apearance"), Description("设置值")] public string Text_Value { get { return lblValue.Text; } set { lblValue.Text = value; this.Width = SetWidth(); } } ////// 单位 /// [Category("Apearance"), Description("设置单位")] public string Text_Unit { get { return lblUnit.Text; } set { lblUnit.Text = value; this.Width = SetWidth(); } } ////// 标签前景色 /// [Category("Apearance"), Description("设置或获取title、value的前景色")] public Color LabelForeColor { get { return lblTitle.ForeColor; } set { lblTitle.ForeColor = value; lblValue.ForeColor = value; } } ////// 标签背景色 /// [Category("Apearance"), Description("设置或获取title、value的背景色")] public Color LabelBackColor { get { return lblTitle.BackColor; } set { lblTitle.BackColor = value; lblValue.BackColor = value; } } ////// 获取或设置用于显示单位的标签背景色 /// [Category("Apearance"), Description("设置或获取unit的背景色")] public Color LabelUnitBackColor { get { return lblUnit.BackColor; } set { lblUnit.BackColor = value; } } ////// 获取或设置用于显示单位的标签前景色 /// [Category("Apearance"), Description("设置或获取unit的前景色")] public Color LabelUnitForeColor { get { return lblUnit.ForeColor; } set { lblUnit.ForeColor = value; } } ////// 设置5个label的Font /// [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(); } } ////// 文字改变事件 /// /// /// 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); } } }}