(六)c#Winform自定义控件-单选框

官网

http://www.hzhcontrols.com

前提

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

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

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

欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

目录

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

准备工作

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

复选框需要支持分组,当同一面板上具有多种选择的时候,分组就显得更为重要了

开始

添加一个用户控件,命名为:UCRadioButton

看一下有哪些属性

  1 [Description("选中改变事件"), Category("自定义")]
  2         public event EventHandler CheckedChangeEvent;
  3 
  4         private Font _Font = new Font("微软雅黑", 12);
  5         [Description("字体"), Category("自定义")]
  6         public new Font Font
  7         {
  8             get { return _Font; }
  9             set
 10             {
 11                 _Font = value;
 12                 label1.Font = value;
 13             }
 14         }
 15 
 16         private Color _ForeColor = Color.FromArgb(62, 62, 62);
 17         [Description("字体颜色"), Category("自定义")]
 18         public new Color ForeColor
 19         {
 20             get { return _ForeColor; }
 21             set
 22             {
 23                 label1.ForeColor = value;
 24                 _ForeColor = value;
 25             }
 26         }
 27         private string _Text = "单选按钮";
 28         [Description("文本"), Category("自定义")]
 29         public string TextValue
 30         {
 31             get { return _Text; }
 32             set
 33             {
 34                 label1.Text = value;
 35                 _Text = value;
 36             }
 37         }
 38         private bool _checked = false;
 39         [Description("是否选中"), Category("自定义")]
 40         public bool Checked
 41         {
 42             get
 43             {
 44                 return _checked;
 45             }
 46             set
 47             {
 48                 if (_checked != value)
 49                 {
 50                     _checked = value;
 51                     if (base.Enabled)
 52                     {
 53                         if (_checked)
 54                         {
 55                             panel1.BackgroundImage = Properties.Resources.radioButton1;
 56                         }
 57                         else
 58                         {
 59                             panel1.BackgroundImage = Properties.Resources.radioButton0;
 60                         }
 61                     }
 62                     else
 63                     {
 64                         if (_checked)
 65                         {
 66                             panel1.BackgroundImage = Properties.Resources.radioButton10;
 67                         }
 68                         else
 69                         {
 70                             panel1.BackgroundImage = Properties.Resources.radioButton00;
 71                         }
 72                     }
 73                     SetCheck(value);
 74 
 75                     if (CheckedChangeEvent != null)
 76                     {
 77                         CheckedChangeEvent(this, null);
 78                     }
 79                 }
 80             }
 81         }
 82 
 83         private string _groupName;
 84 
 85         [Description("分组名称"), Category("自定义")]
 86         public string GroupName
 87         {
 88             get { return _groupName; }
 89             set { _groupName = value; }
 90         }
 91 
 92         public new bool Enabled
 93         {
 94             get
 95             {
 96                 return base.Enabled;
 97             }
 98             set
 99             {
100                 base.Enabled = value;
101                 if (value)
102                 {
103                     if (_checked)
104                     {
105                         panel1.BackgroundImage = Properties.Resources.radioButton1;
106                     }
107                     else
108                     {
109                         panel1.BackgroundImage = Properties.Resources.radioButton0;
110                     }
111                 }
112                 else
113                 {
114                     if (_checked)
115                     {
116                         panel1.BackgroundImage = Properties.Resources.radioButton10;
117                     }
118                     else
119                     {
120                         panel1.BackgroundImage = Properties.Resources.radioButton00;
121                     }
122                 }
123             }
124         }

当选中状态改变时需要根据分组名称来做相应的处理

 1   private void SetCheck(bool bln)
 2         {
 3             if (!bln)
 4                 return;
 5             if (this.Parent != null)
 6             {
 7                 foreach (Control c in this.Parent.Controls)
 8                 {
 9                     if (c is UCRadioButton && c != this)
10                     {
11                         UCRadioButton uc = (UCRadioButton)c;
12                         if (_groupName == uc.GroupName && uc.Checked)
13                         {
14                             uc.Checked = false;
15                             return;
16                         }
17                     }
18                 }
19             }
20         }

当点击时改变选中状态

1         private void Radio_MouseDown(object sender, MouseEventArgs e)
2         {
3             this.Checked = true;
4         }

加载时做一下处理,防止多选了

 1 private void UCRadioButton_Load(object sender, EventArgs e)
 2         {
 3             if (this.Parent != null && this._checked)
 4             {
 5                 foreach (Control c in this.Parent.Controls)
 6                 {
 7                     if (c is UCRadioButton && c != this)
 8                     {
 9                         UCRadioButton uc = (UCRadioButton)c;
10                         if (_groupName == uc.GroupName && uc.Checked)
11                         {
12                             Checked = false;
13                             return;
14                         }
15                     }
16                 }
17             }
18         }

来看下完整的代码吧

  1 // 版权所有  黄正辉  交流群:568015492   QQ:623128629
  2 // 文件名称:UCRadioButton.cs
  3 // 创建日期:2019-08-15 16:03:13
  4 // 功能描述:RadioButton
  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 UCRadioButton : UserControl
 19     {
 20         [Description("选中改变事件"), Category("自定义")]
 21         public event EventHandler CheckedChangeEvent;
 22 
 23         private Font _Font = new Font("微软雅黑", 12);
 24         [Description("字体"), Category("自定义")]
 25         public new Font Font
 26         {
 27             get { return _Font; }
 28             set
 29             {
 30                 _Font = value;
 31                 label1.Font = value;
 32             }
 33         }
 34 
 35         private Color _ForeColor = Color.FromArgb(62, 62, 62);
 36         [Description("字体颜色"), Category("自定义")]
 37         public new Color ForeColor
 38         {
 39             get { return _ForeColor; }
 40             set
 41             {
 42                 label1.ForeColor = value;
 43                 _ForeColor = value;
 44             }
 45         }
 46         private string _Text = "单选按钮";
 47         [Description("文本"), Category("自定义")]
 48         public string TextValue
 49         {
 50             get { return _Text; }
 51             set
 52             {
 53                 label1.Text = value;
 54                 _Text = value;
 55             }
 56         }
 57         private bool _checked = false;
 58         [Description("是否选中"), Category("自定义")]
 59         public bool Checked
 60         {
 61             get
 62             {
 63                 return _checked;
 64             }
 65             set
 66             {
 67                 if (_checked != value)
 68                 {
 69                     _checked = value;
 70                     if (base.Enabled)
 71                     {
 72                         if (_checked)
 73                         {
 74                             panel1.BackgroundImage = Properties.Resources.radioButton1;
 75                         }
 76                         else
 77                         {
 78                             panel1.BackgroundImage = Properties.Resources.radioButton0;
 79                         }
 80                     }
 81                     else
 82                     {
 83                         if (_checked)
 84                         {
 85                             panel1.BackgroundImage = Properties.Resources.radioButton10;
 86                         }
 87                         else
 88                         {
 89                             panel1.BackgroundImage = Properties.Resources.radioButton00;
 90                         }
 91                     }
 92                     SetCheck(value);
 93 
 94                     if (CheckedChangeEvent != null)
 95                     {
 96                         CheckedChangeEvent(this, null);
 97                     }
 98                 }
 99             }
100         }
101 
102         private string _groupName;
103 
104         [Description("分组名称"), Category("自定义")]
105         public string GroupName
106         {
107             get { return _groupName; }
108             set { _groupName = value; }
109         }
110 
111         public new bool Enabled
112         {
113             get
114             {
115                 return base.Enabled;
116             }
117             set
118             {
119                 base.Enabled = value;
120                 if (value)
121                 {
122                     if (_checked)
123                     {
124                         panel1.BackgroundImage = Properties.Resources.radioButton1;
125                     }
126                     else
127                     {
128                         panel1.BackgroundImage = Properties.Resources.radioButton0;
129                     }
130                 }
131                 else
132                 {
133                     if (_checked)
134                     {
135                         panel1.BackgroundImage = Properties.Resources.radioButton10;
136                     }
137                     else
138                     {
139                         panel1.BackgroundImage = Properties.Resources.radioButton00;
140                     }
141                 }
142             }
143         }
144         public UCRadioButton()
145         {
146             InitializeComponent();
147         }
148 
149         private void SetCheck(bool bln)
150         {
151             if (!bln)
152                 return;
153             if (this.Parent != null)
154             {
155                 foreach (Control c in this.Parent.Controls)
156                 {
157                     if (c is UCRadioButton && c != this)
158                     {
159                         UCRadioButton uc = (UCRadioButton)c;
160                         if (_groupName == uc.GroupName && uc.Checked)
161                         {
162                             uc.Checked = false;
163                             return;
164                         }
165                     }
166                 }
167             }
168         }
169 
170         private void Radio_MouseDown(object sender, MouseEventArgs e)
171         {
172             this.Checked = true;
173         }
174 
175         private void UCRadioButton_Load(object sender, EventArgs e)
176         {
177             if (this.Parent != null && this._checked)
178             {
179                 foreach (Control c in this.Parent.Controls)
180                 {
181                     if (c is UCRadioButton && c != this)
182                     {
183                         UCRadioButton uc = (UCRadioButton)c;
184                         if (_groupName == uc.GroupName && uc.Checked)
185                         {
186                             Checked = false;
187                             return;
188                         }
189                     }
190                 }
191             }
192         }
193     }
194 }
View Code
 1 namespace HZH_Controls.Controls
 2 {
 3     partial class UCRadioButton
 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.label1 = new System.Windows.Forms.Label();
32             this.panel1 = new System.Windows.Forms.Panel();
33             this.SuspendLayout();
34             // 
35             // label1
36             // 
37             this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
38             this.label1.Font = new System.Drawing.Font("微软雅黑", 12F);
39             this.label1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(62)))), ((int)(((byte)(62)))), ((int)(((byte)(62)))));
40             this.label1.Location = new System.Drawing.Point(18, 0);
41             this.label1.Name = "label1";
42             this.label1.Padding = new System.Windows.Forms.Padding(5, 0, 0, 0);
43             this.label1.Size = new System.Drawing.Size(215, 30);
44             this.label1.TabIndex = 3;
45             this.label1.Text = "单选按钮";
46             this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
47             this.label1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Radio_MouseDown);
48             // 
49             // panel1
50             // 
51             this.panel1.BackgroundImage = global::HZH_Controls.Properties.Resources.radioButton0;
52             this.panel1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
53             this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
54             this.panel1.Location = new System.Drawing.Point(0, 0);
55             this.panel1.Name = "panel1";
56             this.panel1.Size = new System.Drawing.Size(18, 30);
57             this.panel1.TabIndex = 2;
58             this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Radio_MouseDown);
59             // 
60             // UCRadioButton
61             // 
62             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
63             this.Controls.Add(this.label1);
64             this.Controls.Add(this.panel1);
65             this.Name = "UCRadioButton";
66             this.Size = new System.Drawing.Size(233, 30);
67             this.Load += new System.EventHandler(this.UCRadioButton_Load);
68             this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Radio_MouseDown);
69             this.ResumeLayout(false);
70 
71         }
72 
73         #endregion
74 
75         private System.Windows.Forms.Label label1;
76         private System.Windows.Forms.Panel panel1;
77     }
78 }
View Code

用处及效果

用处:就是单选框

效果:

 

最后的话

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

posted @ 2019-08-16 10:07 冰封一夏 阅读(...) 评论(...) 编辑 收藏
HZHControls控件库官网:http://hzhcontrols.com