(六)c#Winform自定義控件-單選框

官網

http://www.hzhcontrols.comhtml

前提

入行已經7,8年了,一直想作一套漂亮點的自定義控件,因而就有了本系列文章。git

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

碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.git編輯器

若是以爲寫的還行,請點個 star 支持一下吧ide

歡迎前來交流探討: 企鵝羣568015492 企鵝羣568015492字體

目錄

http://www.javashuo.com/article/p-hacmmtru-mw.htmlthis

準備工做

準備4個圖片,分別對應選中,沒選中,選中禁用,沒選中禁用spa

複選框須要支持分組,當同一面板上具備多種選擇的時候,分組就顯得更爲重要了設計

開始

添加一個用戶控件,命名爲:UCRadioButtoncode

看一下有哪些屬性

  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 點個星星吧

相關文章
相關標籤/搜索