UCBtnsGroup.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-15-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCBtnsGroup.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 UCBtnsGroup.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. public partial class UCBtnsGroup : UserControl
  32. {
  33. /// <summary>
  34. /// 选中改变事件
  35. /// </summary>
  36. [Description("选中改变事件"), Category("自定义")]
  37. public event EventHandler SelectedItemChanged;
  38. /// <summary>
  39. /// The m data source
  40. /// </summary>
  41. private Dictionary<string, string> m_dataSource = new Dictionary<string, string>();
  42. /// <summary>
  43. /// 数据源
  44. /// </summary>
  45. /// <value>The data source.</value>
  46. [Description("数据源"), Category("自定义")]
  47. public Dictionary<string, string> DataSource
  48. {
  49. get { return m_dataSource; }
  50. set
  51. {
  52. m_dataSource = value;
  53. Reload();
  54. }
  55. }
  56. /// <summary>
  57. /// The m select item
  58. /// </summary>
  59. private List<string> m_selectItem = new List<string>();
  60. /// <summary>
  61. /// 选中项
  62. /// </summary>
  63. /// <value>The select item.</value>
  64. [Description("选中项"), Category("自定义")]
  65. public List<string> SelectItem
  66. {
  67. get { return m_selectItem; }
  68. set
  69. {
  70. m_selectItem = value;
  71. if (m_selectItem == null)
  72. m_selectItem = new List<string>();
  73. SetSelected();
  74. }
  75. }
  76. /// <summary>
  77. /// The m is multiple
  78. /// </summary>
  79. private bool m_isMultiple = false;
  80. /// <summary>
  81. /// 是否多选
  82. /// </summary>
  83. /// <value><c>true</c> if this instance is multiple; otherwise, <c>false</c>.</value>
  84. [Description("是否多选"), Category("自定义")]
  85. public bool IsMultiple
  86. {
  87. get { return m_isMultiple; }
  88. set { m_isMultiple = value; }
  89. }
  90. /// <summary>
  91. /// Initializes a new instance of the <see cref="UCBtnsGroup" /> class.
  92. /// </summary>
  93. public UCBtnsGroup()
  94. {
  95. InitializeComponent();
  96. }
  97. /// <summary>
  98. /// Reloads this instance.
  99. /// </summary>
  100. private void Reload()
  101. {
  102. try
  103. {
  104. ControlHelper.FreezeControl(flowLayoutPanel1, true);
  105. this.flowLayoutPanel1.Controls.Clear();
  106. if (DataSource != null)
  107. {
  108. foreach (var item in DataSource)
  109. {
  110. UCBtnExt btn = new UCBtnExt();
  111. btn.BackColor = System.Drawing.Color.Transparent;
  112. btn.BtnBackColor = System.Drawing.Color.White;
  113. btn.BtnFont = new System.Drawing.Font("微软雅黑", 10F);
  114. btn.BtnForeColor = System.Drawing.Color.Gray;
  115. btn.BtnText = item.Value;
  116. btn.ConerRadius = 5;
  117. btn.Cursor = System.Windows.Forms.Cursors.Hand;
  118. btn.FillColor = System.Drawing.Color.White;
  119. btn.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
  120. btn.IsRadius = true;
  121. btn.IsShowRect = true;
  122. btn.IsShowTips = false;
  123. btn.Location = new System.Drawing.Point(5, 5);
  124. btn.Margin = new System.Windows.Forms.Padding(5);
  125. btn.Name = item.Key;
  126. btn.RectColor = System.Drawing.Color.FromArgb(224, 224, 224);
  127. btn.RectWidth = 1;
  128. btn.Size = new System.Drawing.Size(72, 38);
  129. btn.TabStop = false;
  130. btn.BtnClick += btn_BtnClick;
  131. this.flowLayoutPanel1.Controls.Add(btn);
  132. }
  133. }
  134. }
  135. finally
  136. {
  137. ControlHelper.FreezeControl(flowLayoutPanel1, false);
  138. }
  139. SetSelected();
  140. }
  141. /// <summary>
  142. /// Handles the BtnClick event of the btn control.
  143. /// </summary>
  144. /// <param name="sender">The source of the event.</param>
  145. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  146. void btn_BtnClick(object sender, EventArgs e)
  147. {
  148. var btn = sender as UCBtnExt;
  149. if (m_selectItem.Contains(btn.Name))
  150. {
  151. btn.RectColor = System.Drawing.Color.FromArgb(224, 224, 224);
  152. m_selectItem.Remove(btn.Name);
  153. }
  154. else
  155. {
  156. if (!m_isMultiple)
  157. {
  158. foreach (var item in m_selectItem)
  159. {
  160. var lst = this.flowLayoutPanel1.Controls.Find(item, false);
  161. if (lst.Length == 1)
  162. {
  163. var _btn = lst[0] as UCBtnExt;
  164. _btn.RectColor = System.Drawing.Color.FromArgb(224, 224, 224);
  165. }
  166. }
  167. m_selectItem.Clear();
  168. }
  169. btn.RectColor = System.Drawing.Color.FromArgb(255, 77, 59);
  170. m_selectItem.Add(btn.Name);
  171. }
  172. if (SelectedItemChanged != null)
  173. SelectedItemChanged(this, e);
  174. }
  175. /// <summary>
  176. /// Sets the selected.
  177. /// </summary>
  178. private void SetSelected()
  179. {
  180. if (m_selectItem != null && m_selectItem.Count > 0 && DataSource != null && DataSource.Count > 0)
  181. {
  182. try
  183. {
  184. ControlHelper.FreezeControl(flowLayoutPanel1, true);
  185. if (m_isMultiple)
  186. {
  187. foreach (var item in m_selectItem)
  188. {
  189. var lst = this.flowLayoutPanel1.Controls.Find(item, false);
  190. if (lst.Length == 1)
  191. {
  192. var btn = lst[0] as UCBtnExt;
  193. btn.RectColor = System.Drawing.Color.FromArgb(255, 77, 59);
  194. }
  195. }
  196. }
  197. else
  198. {
  199. UCBtnExt btn = null;
  200. foreach (var item in m_selectItem)
  201. {
  202. var lst = this.flowLayoutPanel1.Controls.Find(item, false);
  203. if (lst.Length == 1)
  204. {
  205. btn = lst[0] as UCBtnExt;
  206. break;
  207. }
  208. }
  209. if (btn != null)
  210. {
  211. m_selectItem = new List<string>() { btn.Name };
  212. btn.RectColor = System.Drawing.Color.FromArgb(255, 77, 59);
  213. }
  214. }
  215. }
  216. finally
  217. {
  218. ControlHelper.FreezeControl(flowLayoutPanel1, false);
  219. }
  220. }
  221. }
  222. }
  223. }