UCHorizontalList.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCHorizontalList.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 UCHorizontalList.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. public partial class UCHorizontalList : UserControl
  32. {
  33. /// <summary>
  34. /// Gets or sets the selected item.
  35. /// </summary>
  36. /// <value>The selected item.</value>
  37. public UCHorizontalListItem SelectedItem { get; set; }
  38. /// <summary>
  39. /// Occurs when [selected item event].
  40. /// </summary>
  41. public event EventHandler SelectedItemEvent;
  42. /// <summary>
  43. /// The m start item index
  44. /// </summary>
  45. private int m_startItemIndex = 0;
  46. /// <summary>
  47. /// The is automatic select first
  48. /// </summary>
  49. private bool isAutoSelectFirst = true;
  50. /// <summary>
  51. /// Gets or sets a value indicating whether this instance is automatic select first.
  52. /// </summary>
  53. /// <value><c>true</c> if this instance is automatic select first; otherwise, <c>false</c>.</value>
  54. public bool IsAutoSelectFirst
  55. {
  56. get { return isAutoSelectFirst; }
  57. set { isAutoSelectFirst = value; }
  58. }
  59. /// <summary>
  60. /// The data source
  61. /// </summary>
  62. private List<KeyValuePair<string, string>> dataSource = null;
  63. /// <summary>
  64. /// Gets or sets the data source.
  65. /// </summary>
  66. /// <value>The data source.</value>
  67. public List<KeyValuePair<string, string>> DataSource
  68. {
  69. get { return dataSource; }
  70. set
  71. {
  72. dataSource = value;
  73. ReloadSource();
  74. }
  75. }
  76. private Color selectedColor = Color.FromArgb(255, 77, 59);
  77. [Description("选中颜色"), Category("自定义")]
  78. public Color SelectedColor
  79. {
  80. get { return selectedColor; }
  81. set { selectedColor = value; }
  82. }
  83. /// <summary>
  84. /// Initializes a new instance of the <see cref="UCHorizontalList" /> class.
  85. /// </summary>
  86. public UCHorizontalList()
  87. {
  88. InitializeComponent();
  89. }
  90. /// <summary>
  91. /// Reloads the source.
  92. /// </summary>
  93. public void ReloadSource()
  94. {
  95. try
  96. {
  97. ControlHelper.FreezeControl(this, true);
  98. this.panList.SuspendLayout();
  99. this.panList.Controls.Clear();
  100. this.panList.Width = this.panMain.Width;
  101. if (DataSource != null)
  102. {
  103. foreach (var item in DataSource)
  104. {
  105. UCHorizontalListItem uc = new UCHorizontalListItem();
  106. uc.SelectedColor = selectedColor;
  107. uc.DataSource = item;
  108. uc.SelectedItem += uc_SelectItem;
  109. this.panList.Controls.Add(uc);
  110. }
  111. }
  112. this.panList.ResumeLayout(true);
  113. if (this.panList.Controls.Count > 0)
  114. this.panList.Width = panMain.Width + this.panList.Controls[0].Location.X * -1;
  115. this.panList.Location = new Point(0, 0);
  116. m_startItemIndex = 0;
  117. if (this.panList.Width > panMain.Width)
  118. panRight.Visible = true;
  119. else
  120. panRight.Visible = false;
  121. panLeft.Visible = false;
  122. panList.SendToBack();
  123. panRight.SendToBack();
  124. if (isAutoSelectFirst && DataSource != null && DataSource.Count > 0)
  125. {
  126. SelectItem((UCHorizontalListItem)this.panList.Controls[0]);
  127. }
  128. }
  129. finally
  130. {
  131. ControlHelper.FreezeControl(this, false);
  132. }
  133. }
  134. /// <summary>
  135. /// Handles the SelectItem event of the uc control.
  136. /// </summary>
  137. /// <param name="sender">The source of the event.</param>
  138. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  139. void uc_SelectItem(object sender, EventArgs e)
  140. {
  141. SelectItem(sender as UCHorizontalListItem);
  142. }
  143. /// <summary>
  144. /// Selects the item.
  145. /// </summary>
  146. /// <param name="item">The item.</param>
  147. private void SelectItem(UCHorizontalListItem item)
  148. {
  149. if (SelectedItem != null && !SelectedItem.IsDisposed)
  150. SelectedItem.SetSelect(false);
  151. SelectedItem = item;
  152. SelectedItem.SetSelect(true);
  153. if (SelectedItemEvent != null)
  154. SelectedItemEvent(item, null);
  155. }
  156. /// <summary>
  157. /// Handles the MouseDown event of the panLeft control.
  158. /// </summary>
  159. /// <param name="sender">The source of the event.</param>
  160. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  161. private void panLeft_MouseDown(object sender, MouseEventArgs e)
  162. {
  163. if (this.panList.Location.X >= 0)
  164. {
  165. this.panList.Location = new Point(0, 0);
  166. return;
  167. }
  168. for (int i = m_startItemIndex; i >= 0; i--)
  169. {
  170. if (this.panList.Controls[i].Location.X < this.panList.Controls[m_startItemIndex].Location.X - panMain.Width)
  171. {
  172. m_startItemIndex = i + 1;
  173. break; ;
  174. }
  175. if (i == 0)
  176. {
  177. m_startItemIndex = 0;
  178. }
  179. }
  180. ResetListLocation();
  181. panRight.Visible = true;
  182. if (this.panList.Location.X >= 0)
  183. {
  184. panLeft.Visible = false;
  185. }
  186. else
  187. {
  188. panLeft.Visible = true;
  189. }
  190. panList.SendToBack();
  191. panRight.SendToBack();
  192. }
  193. /// <summary>
  194. /// Handles the MouseDown event of the panRight control.
  195. /// </summary>
  196. /// <param name="sender">The source of the event.</param>
  197. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  198. private void panRight_MouseDown(object sender, MouseEventArgs e)
  199. {
  200. if (this.panList.Location.X + this.panList.Width <= this.panMain.Width)
  201. return;
  202. if (this.panList.Controls.Count <= 0)
  203. return;
  204. for (int i = m_startItemIndex; i < this.panList.Controls.Count; i++)
  205. {
  206. if (this.panList.Location.X + this.panList.Controls[i].Location.X + this.panList.Controls[i].Width > panMain.Width)
  207. {
  208. m_startItemIndex = i;
  209. break;
  210. }
  211. }
  212. ResetListLocation();
  213. panLeft.Visible = true;
  214. if (panList.Width + panList.Location.X <= panMain.Width)
  215. panRight.Visible = false;
  216. else
  217. panRight.Visible = true;
  218. panList.SendToBack();
  219. panRight.SendToBack();
  220. }
  221. /// <summary>
  222. /// Resets the list location.
  223. /// </summary>
  224. private void ResetListLocation()
  225. {
  226. if (this.panList.Controls.Count > 0)
  227. {
  228. this.panList.Location = new Point(this.panList.Controls[m_startItemIndex].Location.X * -1, 0);
  229. }
  230. }
  231. /// <summary>
  232. /// Sets the select.
  233. /// </summary>
  234. /// <param name="strKey">The string key.</param>
  235. public void SetSelect(string strKey)
  236. {
  237. foreach (UCHorizontalListItem item in this.panList.Controls)
  238. {
  239. if (item.DataSource.Key == strKey)
  240. {
  241. SelectItem(item);
  242. return;
  243. }
  244. }
  245. }
  246. }
  247. }