UCRadioButton.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCRadioButton.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Drawing;
  20. using System.Data;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCRadioButton.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. [DefaultEvent("CheckedChangeEvent")]
  32. public partial class UCRadioButton : UserControl
  33. {
  34. /// <summary>
  35. /// Occurs when [checked change event].
  36. /// </summary>
  37. [Description("选中改变事件"), Category("自定义")]
  38. public event EventHandler CheckedChangeEvent;
  39. /// <summary>
  40. /// The font
  41. /// </summary>
  42. private Font _Font = new Font("微软雅黑", 12);
  43. /// <summary>
  44. /// 获取或设置控件显示的文字的字体。
  45. /// </summary>
  46. /// <value>The font.</value>
  47. /// <PermissionSet>
  48. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  49. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  50. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  51. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  52. /// </PermissionSet>
  53. [Description("字体"), Category("自定义")]
  54. public new Font Font
  55. {
  56. get { return _Font; }
  57. set
  58. {
  59. _Font = value;
  60. label1.Font = value;
  61. }
  62. }
  63. /// <summary>
  64. /// The fore color
  65. /// </summary>
  66. private Color _ForeColor = Color.FromArgb(62, 62, 62);
  67. /// <summary>
  68. /// 获取或设置控件的前景色。
  69. /// </summary>
  70. /// <value>The color of the fore.</value>
  71. /// <PermissionSet>
  72. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  73. /// </PermissionSet>
  74. [Description("字体颜色"), Category("自定义")]
  75. public new Color ForeColor
  76. {
  77. get { return _ForeColor; }
  78. set
  79. {
  80. label1.ForeColor = value;
  81. _ForeColor = value;
  82. }
  83. }
  84. /// <summary>
  85. /// The text
  86. /// </summary>
  87. private string _Text = "单选按钮";
  88. /// <summary>
  89. /// Gets or sets the text value.
  90. /// </summary>
  91. /// <value>The text value.</value>
  92. [Description("文本"), Category("自定义")]
  93. public string TextValue
  94. {
  95. get { return _Text; }
  96. set
  97. {
  98. label1.Text = value;
  99. _Text = value;
  100. }
  101. }
  102. /// <summary>
  103. /// The checked
  104. /// </summary>
  105. private bool _checked = false;
  106. /// <summary>
  107. /// Gets or sets a value indicating whether this <see cref="UCRadioButton" /> is checked.
  108. /// </summary>
  109. /// <value><c>true</c> if checked; otherwise, <c>false</c>.</value>
  110. [Description("是否选中"), Category("自定义")]
  111. public bool Checked
  112. {
  113. get
  114. {
  115. return _checked;
  116. }
  117. set
  118. {
  119. if (_checked != value)
  120. {
  121. _checked = value;
  122. if (base.Enabled)
  123. {
  124. if (_checked)
  125. {
  126. panel1.BackgroundImage = Properties.Resources.radioButton1;
  127. }
  128. else
  129. {
  130. panel1.BackgroundImage = Properties.Resources.radioButton0;
  131. }
  132. }
  133. else
  134. {
  135. if (_checked)
  136. {
  137. panel1.BackgroundImage = Properties.Resources.radioButton10;
  138. }
  139. else
  140. {
  141. panel1.BackgroundImage = Properties.Resources.radioButton00;
  142. }
  143. }
  144. SetCheck(value);
  145. if (CheckedChangeEvent != null)
  146. {
  147. CheckedChangeEvent(this, null);
  148. }
  149. }
  150. }
  151. }
  152. /// <summary>
  153. /// The group name
  154. /// </summary>
  155. private string _groupName;
  156. /// <summary>
  157. /// Gets or sets the name of the group.
  158. /// </summary>
  159. /// <value>The name of the group.</value>
  160. [Description("分组名称"), Category("自定义")]
  161. public string GroupName
  162. {
  163. get { return _groupName; }
  164. set { _groupName = value; }
  165. }
  166. /// <summary>
  167. /// 获取或设置一个值,该值指示控件是否可以对用户交互作出响应。
  168. /// </summary>
  169. /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
  170. /// <PermissionSet>
  171. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  172. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  173. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  174. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  175. /// </PermissionSet>
  176. public new bool Enabled
  177. {
  178. get
  179. {
  180. return base.Enabled;
  181. }
  182. set
  183. {
  184. base.Enabled = value;
  185. if (value)
  186. {
  187. if (_checked)
  188. {
  189. panel1.BackgroundImage = Properties.Resources.radioButton1;
  190. }
  191. else
  192. {
  193. panel1.BackgroundImage = Properties.Resources.radioButton0;
  194. }
  195. }
  196. else
  197. {
  198. if (_checked)
  199. {
  200. panel1.BackgroundImage = Properties.Resources.radioButton10;
  201. }
  202. else
  203. {
  204. panel1.BackgroundImage = Properties.Resources.radioButton00;
  205. }
  206. }
  207. }
  208. }
  209. /// <summary>
  210. /// Initializes a new instance of the <see cref="UCRadioButton" /> class.
  211. /// </summary>
  212. public UCRadioButton()
  213. {
  214. InitializeComponent();
  215. }
  216. /// <summary>
  217. /// Sets the check.
  218. /// </summary>
  219. /// <param name="bln">if set to <c>true</c> [BLN].</param>
  220. private void SetCheck(bool bln)
  221. {
  222. if (!bln)
  223. return;
  224. if (this.Parent != null)
  225. {
  226. foreach (Control c in this.Parent.Controls)
  227. {
  228. if (c is UCRadioButton && c != this)
  229. {
  230. UCRadioButton uc = (UCRadioButton)c;
  231. if (_groupName == uc.GroupName && uc.Checked)
  232. {
  233. uc.Checked = false;
  234. return;
  235. }
  236. }
  237. }
  238. }
  239. }
  240. /// <summary>
  241. /// Handles the MouseDown event of the Radio control.
  242. /// </summary>
  243. /// <param name="sender">The source of the event.</param>
  244. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  245. private void Radio_MouseDown(object sender, MouseEventArgs e)
  246. {
  247. this.Checked = true;
  248. }
  249. /// <summary>
  250. /// Handles the Load event of the UCRadioButton control.
  251. /// </summary>
  252. /// <param name="sender">The source of the event.</param>
  253. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  254. private void UCRadioButton_Load(object sender, EventArgs e)
  255. {
  256. if (this.Parent != null && this._checked)
  257. {
  258. foreach (Control c in this.Parent.Controls)
  259. {
  260. if (c is UCRadioButton && c != this)
  261. {
  262. UCRadioButton uc = (UCRadioButton)c;
  263. if (_groupName == uc.GroupName && uc.Checked)
  264. {
  265. Checked = false;
  266. return;
  267. }
  268. }
  269. }
  270. }
  271. }
  272. }
  273. }