前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
<https://gitee.com/kwwwvagaa/net_winform_custom_control>

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 
<https://shang.qq.com/wpa/qunwpa?idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d>

目录

https://www.cnblogs.com/bfyx/p/11364884.html
<https://www.cnblogs.com/bfyx/p/11364884.html>

准备工作

准备4个图片,分别对应选中,没选中,选中禁用,没选中禁用



 

开始

 新增一个用户控件,命名UCCheckBox

属性如下
1 [Description("选中改变事件"), Category("自定义")] 2 public event EventHandler
CheckedChangeEvent; 3 4 [Description("字体"), Category("自定义")] 5 public override
Font Font 6 { 7 get 8 { 9 return base.Font; 10 } 11 set 12 { 13 base
.Font = value; 14 label1.Font = value; 15 } 16 } 17 18 private Color
_ForeColor = Color.FromArgb(62, 62, 62); 19 [Description("字体颜色"), Category("自定义
")] 20 public new Color ForeColor 21 { 22 get { return _ForeColor; } 23 set
24 { 25 label1.ForeColor = value; 26 _ForeColor = value; 27 } 28 } 29
private string _Text = "复选框"; 30 [Description("文本"), Category("自定义")] 31
public string TextValue 32 { 33 get { return _Text; } 34 set 35 { 36
label1.Text = value; 37 _Text = value; 38 } 39 } 40 private bool _checked
=false; 41 [Description("是否选中"), Category("自定义")] 42 public bool Checked 43
{ 44 get 45 { 46 return _checked; 47 } 48 set 49 { 50 if (_checked !=
value) 51 { 52 _checked = value; 53 if (base.Enabled) 54 { 55 if
(_checked) 56 { 57 panel1.BackgroundImage = Properties.Resources.checkbox1;
58 } 59 else 60 { 61 panel1.BackgroundImage =
Properties.Resources.checkbox0; 62 } 63 } 64 else 65 { 66 if (_checked)
67 { 68 panel1.BackgroundImage = Properties.Resources.checkbox10; 69 } 70
else 71 { 72 panel1.BackgroundImage = Properties.Resources.checkbox00; 73 }
74 } 75 76 if (CheckedChangeEvent != null) 77 { 78 CheckedChangeEvent(
this, null); 79 } 80 } 81 } 82 } 83 84 public new bool Enabled 85 {
86 get 87 { 88 return base.Enabled; 89 } 90 set 91 { 92 base.Enabled =
value; 93 if (value) 94 { 95 if (_checked) 96 { 97
panel1.BackgroundImage = Properties.Resources.checkbox1; 98 } 99 else 100 {
101 panel1.BackgroundImage = Properties.Resources.checkbox0; 102 } 103 } 104
else 105 { 106 if (_checked) 107 { 108 panel1.BackgroundImage =
Properties.Resources.checkbox10;109 } 110 else 111 { 112
panel1.BackgroundImage = Properties.Resources.checkbox00; 113 } 114 } 115 }
116 }
再来一个改变选中状态的事件
1 private void CheckBox_MouseDown(object sender, MouseEventArgs e) 2 { 3
Checked = !Checked; 4 }
下面看下完整代码
1 // 版权所有 黄正辉 交流群:568015492 QQ:623128629 2 // 文件名称:UCCheckBox.cs 3 //
创建日期:2019-08-15 15:58:34 4 // 功能描述:CheckBox 5 // 项目地址:
https://gitee.com/kwwwvagaa/net_winform_custom_control 6 using System; 7 using
System.Collections.Generic; 8 using System.ComponentModel; 9 using
System.Drawing; 10 using System.Data; 11 using System.Linq; 12 using
System.Text; 13 using System.Windows.Forms; 14 15 namespace
HZH_Controls.Controls 16 { 17 [DefaultEvent("CheckedChangeEvent")] 18 public
partial class UCCheckBox : UserControl 19 { 20 [Description("选中改变事件"),
Category("自定义")] 21 public event EventHandler CheckedChangeEvent; 22 23
[Description("字体"), Category("自定义")] 24 public override Font Font 25 { 26
get 27 { 28 return base.Font; 29 } 30 set 31 { 32 base.Font = value;
33 label1.Font = value; 34 } 35 } 36 37 private Color _ForeColor =
Color.FromArgb(62, 62, 62); 38 [Description("字体颜色"), Category("自定义")] 39
public new Color ForeColor 40 { 41 get { return _ForeColor; } 42 set 43 {
44 label1.ForeColor = value; 45 _ForeColor = value; 46 } 47 } 48 private
string _Text = "复选框"; 49 [Description("文本"), Category("自定义")] 50 public string
TextValue 51 { 52 get { return _Text; } 53 set 54 { 55 label1.Text =
value; 56 _Text = value; 57 } 58 } 59 private bool _checked = false; 60
[Description("是否选中"), Category("自定义")] 61 public bool Checked 62 { 63 get
64 { 65 return _checked; 66 } 67 set 68 { 69 if (_checked != value) 70
{ 71 _checked = value; 72 if (base.Enabled) 73 { 74 if (_checked) 75 {
76 panel1.BackgroundImage = Properties.Resources.checkbox1; 77 } 78 else 79
{ 80 panel1.BackgroundImage = Properties.Resources.checkbox0; 81 } 82 } 83
else 84 { 85 if (_checked) 86 { 87 panel1.BackgroundImage =
Properties.Resources.checkbox10; 88 } 89 else 90 { 91
panel1.BackgroundImage = Properties.Resources.checkbox00; 92 } 93 } 94 95
if (CheckedChangeEvent != null) 96 { 97 CheckedChangeEvent(this, null); 98
} 99 } 100 } 101 } 102 103 public new bool Enabled 104 { 105 get 106 { 107
return base.Enabled; 108 } 109 set 110 { 111 base.Enabled = value; 112 if
(value)113 { 114 if (_checked) 115 { 116 panel1.BackgroundImage =
Properties.Resources.checkbox1;117 } 118 else 119 { 120
panel1.BackgroundImage = Properties.Resources.checkbox0; 121 } 122 } 123 else
124 { 125 if (_checked) 126 { 127 panel1.BackgroundImage =
Properties.Resources.checkbox10;128 } 129 else 130 { 131
panel1.BackgroundImage = Properties.Resources.checkbox00; 132 } 133 } 134 }
135 } 136 public UCCheckBox() 137 { 138 InitializeComponent(); 139 } 140 141
private void CheckBox_MouseDown(object sender, MouseEventArgs e) 142 { 143
Checked = !Checked; 144 } 145 } 146 } View Code 1 namespace
HZH_Controls.Controls 2 { 3 partial class UCCheckBox 4 { 5 /// <summary> 6
/// 必需的设计器变量。 7 /// </summary> 8 private System.ComponentModel.IContainer
components =null; 9 10 /// <summary> 11 /// 清理所有正在使用的资源。 12 /// </summary> 13
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 14 protected
override void Dispose(bool disposing) 15 { 16 if (disposing && (components !=
null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 }
22 23 #region 组件设计器生成的代码 24 25 /// <summary> 26 /// 设计器支持所需的方法 - 不要 27 ///
使用代码编辑器修改此方法的内容。28 /// </summary> 29 private void InitializeComponent() 30 { 31
this.panel1 = new System.Windows.Forms.Panel(); 32 this.label1 = new
System.Windows.Forms.Label();33 this.SuspendLayout(); 34 // 35 // panel1 36 //
37 this.panel1.BackgroundImage = global
::HZH_Controls.Properties.Resources.checkbox0;38 this
.panel1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; 39 this
.panel1.Dock = System.Windows.Forms.DockStyle.Left; 40 this.panel1.Location =
new System.Drawing.Point(1, 1); 41 this.panel1.Name = "panel1"; 42 this
.panel1.Size =new System.Drawing.Size(18, 28); 43 this.panel1.TabIndex = 0; 44
this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this
.CheckBox_MouseDown);45 // 46 // label1 47 // 48 this.label1.Dock =
System.Windows.Forms.DockStyle.Fill;49 this.label1.Font = new
System.Drawing.Font("微软雅黑", 12F); 50 this.label1.ForeColor =
System.Drawing.Color.FromArgb(((int)(((byte)(62)))), ((int)(((byte)(62)))), ((
int)(((byte)(62))))); 51 this.label1.Location = new System.Drawing.Point(19, 1);
52 this.label1.Name = "label1"; 53 this.label1.Padding = new
System.Windows.Forms.Padding(5, 0, 0, 0); 54 this.label1.Size = new
System.Drawing.Size(213, 28); 55 this.label1.TabIndex = 1; 56 this.label1.Text =
"复选框"; 57 this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; 58
this.label1.MouseDown += new System.Windows.Forms.MouseEventHandler(this
.CheckBox_MouseDown);59 // 60 // UCCheckBox 61 // 62 this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.None;63 this.BackColor =
System.Drawing.Color.Transparent;64 this.Controls.Add(this.label1); 65 this
.Controls.Add(this.panel1); 66 this.Name = "UCCheckBox"; 67 this.Padding = new
System.Windows.Forms.Padding(1); 68 this.Size = new System.Drawing.Size(233, 30
);69 this.MouseDown += new System.Windows.Forms.MouseEventHandler(this
.CheckBox_MouseDown);70 this.ResumeLayout(false); 71 72 } 73 74 #endregion 75
76 private System.Windows.Forms.Panel panel1; 77 private
System.Windows.Forms.Label label1;78 } 79 } View Code
用处及效果

用途:复选框

效果:



最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control
<https://gitee.com/kwwwvagaa/net_winform_custom_control> 点个星星吧

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信