UCTimePanel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCTimePanel.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 UCTimePanel.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. [ToolboxItem(false)]
  32. public partial class UCTimePanel : UserControl
  33. {
  34. /// <summary>
  35. /// Occurs when [select source event].
  36. /// </summary>
  37. public event EventHandler SelectSourceEvent;
  38. /// <summary>
  39. /// The source
  40. /// </summary>
  41. private List<KeyValuePair<string, string>> source = null;
  42. /// <summary>
  43. /// Gets or sets a value indicating whether [first event].
  44. /// </summary>
  45. /// <value><c>true</c> if [first event]; otherwise, <c>false</c>.</value>
  46. public bool FirstEvent { get; set; }
  47. /// <summary>
  48. /// Gets or sets the source.
  49. /// </summary>
  50. /// <value>The source.</value>
  51. public List<KeyValuePair<string, string>> Source
  52. {
  53. get { return source; }
  54. set
  55. {
  56. source = value;
  57. SetSource(value);
  58. }
  59. }
  60. /// <summary>
  61. /// The is show border
  62. /// </summary>
  63. private bool _IsShowBorder = false;
  64. /// <summary>
  65. /// Gets or sets a value indicating whether this instance is show border.
  66. /// </summary>
  67. /// <value><c>true</c> if this instance is show border; otherwise, <c>false</c>.</value>
  68. public bool IsShowBorder
  69. {
  70. get { return _IsShowBorder; }
  71. set
  72. {
  73. _IsShowBorder = value;
  74. ucSplitLine_H1.Visible = value;
  75. ucSplitLine_H2.Visible = value;
  76. ucSplitLine_V1.Visible = value;
  77. ucSplitLine_V2.Visible = value;
  78. }
  79. }
  80. /// <summary>
  81. /// The select BTN
  82. /// </summary>
  83. UCBtnExt selectBtn;
  84. /// <summary>
  85. /// 选中按钮
  86. /// </summary>
  87. /// <value>The select BTN.</value>
  88. public UCBtnExt SelectBtn
  89. {
  90. get { return selectBtn; }
  91. set
  92. {
  93. if (selectBtn != null && !selectBtn.IsDisposed)
  94. {
  95. selectBtn.FillColor = System.Drawing.Color.White;
  96. selectBtn.RectColor = System.Drawing.Color.White;
  97. selectBtn.BtnForeColor = System.Drawing.Color.FromArgb(66, 66, 66);
  98. }
  99. bool blnEvent = FirstEvent ? true : (selectBtn != null);
  100. selectBtn = value;
  101. if (value != null)
  102. {
  103. selectBtn.FillColor = System.Drawing.Color.FromArgb(255, 77, 59);
  104. selectBtn.RectColor = System.Drawing.Color.FromArgb(255, 77, 59);
  105. selectBtn.BtnForeColor = System.Drawing.Color.White;
  106. if (blnEvent && SelectSourceEvent != null)
  107. SelectSourceEvent(selectBtn.Tag.ToStringExt(), null);
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// Initializes a new instance of the <see cref="UCTimePanel" /> class.
  113. /// </summary>
  114. public UCTimePanel()
  115. {
  116. InitializeComponent();
  117. this.SizeChanged += UCTimePanel_SizeChanged;
  118. }
  119. /// <summary>
  120. /// Handles the SizeChanged event of the UCTimePanel control.
  121. /// </summary>
  122. /// <param name="sender">The source of the event.</param>
  123. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  124. void UCTimePanel_SizeChanged(object sender, EventArgs e)
  125. {
  126. }
  127. /// <summary>
  128. /// The row
  129. /// </summary>
  130. private int row = 0;
  131. /// <summary>
  132. /// Gets or sets the row.
  133. /// </summary>
  134. /// <value>The row.</value>
  135. public int Row
  136. {
  137. get { return row; }
  138. set
  139. {
  140. row = value;
  141. ReloadPanel();
  142. }
  143. }
  144. /// <summary>
  145. /// The column
  146. /// </summary>
  147. private int column = 0;
  148. /// <summary>
  149. /// Gets or sets the column.
  150. /// </summary>
  151. /// <value>The column.</value>
  152. public int Column
  153. {
  154. get { return column; }
  155. set
  156. {
  157. column = value;
  158. ReloadPanel();
  159. }
  160. }
  161. /// <summary>
  162. /// Handles the Load event of the UCTimePanel control.
  163. /// </summary>
  164. /// <param name="sender">The source of the event.</param>
  165. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  166. private void UCTimePanel_Load(object sender, EventArgs e)
  167. {
  168. }
  169. #region 设置面板数据源
  170. /// <summary>
  171. /// 功能描述:设置面板数据源
  172. /// 作  者:HZH
  173. /// 创建日期:2019-06-25 15:02:15
  174. /// 任务编号:POS
  175. /// </summary>
  176. /// <param name="lstSource">lstSource</param>
  177. public void SetSource(List<KeyValuePair<string, string>> lstSource)
  178. {
  179. try
  180. {
  181. ControlHelper.FreezeControl(this, true);
  182. if (row <= 0 || column <= 0)
  183. return;
  184. if (Source != lstSource)
  185. Source = lstSource;
  186. int index = 0;
  187. SelectBtn = null;
  188. foreach (UCBtnExt btn in this.panMain.Controls)
  189. {
  190. if (lstSource != null && index < lstSource.Count)
  191. {
  192. btn.BtnText = lstSource[index].Value;
  193. btn.Tag = lstSource[index].Key;
  194. index++;
  195. }
  196. else
  197. {
  198. btn.BtnText = "";
  199. btn.Tag = null;
  200. }
  201. }
  202. }
  203. finally
  204. {
  205. ControlHelper.FreezeControl(this, false);
  206. }
  207. }
  208. #endregion
  209. /// <summary>
  210. /// 设置选中项
  211. /// </summary>
  212. /// <param name="strKey">The string key.</param>
  213. public void SetSelect(string strKey)
  214. {
  215. foreach (UCBtnExt item in this.panMain.Controls)
  216. {
  217. if (item.Tag != null && item.Tag.ToStringExt() == strKey)
  218. {
  219. SelectBtn = item;
  220. return;
  221. }
  222. }
  223. SelectBtn = new UCBtnExt();
  224. }
  225. #region 重置面板
  226. /// <summary>
  227. /// 功能描述:重置面板
  228. /// 作  者:HZH
  229. /// 创建日期:2019-06-25 15:02:05
  230. /// 任务编号:POS
  231. /// </summary>
  232. public void ReloadPanel()
  233. {
  234. if (row <= 0 || column <= 0)
  235. return;
  236. SelectBtn = null;
  237. this.panMain.Controls.Clear();
  238. this.panMain.ColumnCount = column;
  239. this.panMain.ColumnStyles.Clear();
  240. for (int i = 0; i < column; i++)
  241. {
  242. this.panMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
  243. }
  244. this.panMain.RowCount = row;
  245. this.panMain.RowStyles.Clear();
  246. for (int i = 0; i < row; i++)
  247. {
  248. this.panMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
  249. }
  250. for (int i = 0; i < row; i++)
  251. {
  252. for (int j = 0; j < column; j++)
  253. {
  254. UCBtnExt btn = new UCBtnExt();
  255. btn.BackColor = System.Drawing.Color.Transparent;
  256. btn.BtnBackColor = System.Drawing.Color.Transparent;
  257. btn.BtnFont = new System.Drawing.Font("微软雅黑", 13F);
  258. btn.BtnForeColor = System.Drawing.Color.FromArgb(66, 66, 66);
  259. btn.ConerRadius = 5;
  260. btn.Dock = DockStyle.Fill;
  261. btn.FillColor = System.Drawing.Color.White;
  262. btn.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
  263. btn.Cursor = Cursor.Current;
  264. btn.IsShowRect = true;
  265. btn.IsRadius = true;
  266. btn.IsShowTips = false;
  267. btn.Name = "btn_" + i + "_" + j;
  268. btn.RectColor = System.Drawing.Color.White;
  269. btn.RectWidth = 1;
  270. btn.Width = this.Width;
  271. btn.TabIndex = 0;
  272. btn.TipsText = "";
  273. btn.BtnClick += btn_BtnClick;
  274. this.panMain.Controls.Add(btn, j, i);
  275. }
  276. }
  277. if (Source != null)
  278. {
  279. SetSource(Source);
  280. }
  281. }
  282. #endregion
  283. /// <summary>
  284. /// Handles the BtnClick event of the btn control.
  285. /// </summary>
  286. /// <param name="sender">The source of the event.</param>
  287. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  288. void btn_BtnClick(object sender, EventArgs e)
  289. {
  290. var btn = (UCBtnExt)sender;
  291. if (btn.Tag == null)
  292. return;
  293. SelectBtn = btn;
  294. }
  295. }
  296. }