UCPagerControlBase.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCPagerControlBase.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 UCPagerControlBase.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// Implements the <see cref="HZH_Controls.Controls.IPageControl" />
  30. /// </summary>
  31. /// <seealso cref="System.Windows.Forms.UserControl" />
  32. /// <seealso cref="HZH_Controls.Controls.IPageControl" />
  33. [ToolboxItem(false)]
  34. public class UCPagerControlBase : UserControl, IPageControl
  35. {
  36. #region 构造
  37. /// <summary>
  38. /// 必需的设计器变量。
  39. /// </summary>
  40. private System.ComponentModel.IContainer components = null;
  41. /// <summary>
  42. /// 清理所有正在使用的资源。
  43. /// </summary>
  44. /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  45. protected override void Dispose(bool disposing)
  46. {
  47. if (disposing && (components != null))
  48. {
  49. components.Dispose();
  50. }
  51. base.Dispose(disposing);
  52. }
  53. #region 组件设计器生成的代码
  54. /// <summary>
  55. /// 设计器支持所需的方法 - 不要
  56. /// 使用代码编辑器修改此方法的内容。
  57. /// </summary>
  58. private void InitializeComponent()
  59. {
  60. this.SuspendLayout();
  61. //
  62. // UCPagerControlBase
  63. //
  64. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  65. this.Name = "UCPagerControlBase";
  66. this.Size = new System.Drawing.Size(304, 58);
  67. this.Load += new System.EventHandler(this.UCPagerControlBase_Load);
  68. this.ResumeLayout(false);
  69. }
  70. #endregion
  71. #endregion
  72. private PageModel m_pm = PageModel.Soure;
  73. /// <summary>
  74. /// 翻页控件模式
  75. /// </summary>
  76. /// <value>The page model.</value>
  77. public PageModel PageModel
  78. {
  79. get { return m_pm; }
  80. set { m_pm = value; }
  81. }
  82. /// <summary>
  83. /// 总页数
  84. /// </summary>
  85. /// <value>The page count.</value>
  86. public virtual int PageCount
  87. {
  88. get;
  89. set;
  90. }
  91. /// <summary>
  92. /// The m page index
  93. /// </summary>
  94. private int m_pageIndex = 1;
  95. /// <summary>
  96. /// 当前页码
  97. /// </summary>
  98. /// <value>The index of the page.</value>
  99. public virtual int PageIndex
  100. {
  101. get { return m_pageIndex; }
  102. set { m_pageIndex = value; }
  103. }
  104. private List<object> dataSource;
  105. /// <summary>
  106. /// 关联的数据源
  107. /// </summary>
  108. /// <value>The data source.</value>
  109. public virtual List<object> DataSource
  110. {
  111. get { return dataSource; }
  112. set
  113. {
  114. dataSource = value;
  115. }
  116. }
  117. /// <summary>
  118. /// 数据源改变时发生
  119. /// </summary>
  120. public virtual event PageControlEventHandler ShowSourceChanged;
  121. /// <summary>
  122. /// The m page size
  123. /// </summary>
  124. private int m_pageSize = 10;
  125. /// <summary>
  126. /// 每页显示数量
  127. /// </summary>
  128. /// <value>The size of the page.</value>
  129. [Description("每页显示数量"), Category("自定义")]
  130. public virtual int PageSize
  131. {
  132. get { return m_pageSize; }
  133. set { m_pageSize = value; }
  134. }
  135. /// <summary>
  136. /// The start index
  137. /// </summary>
  138. private int startIndex = 0;
  139. /// <summary>
  140. /// 开始的下标
  141. /// </summary>
  142. /// <value>The start index.</value>
  143. [Description("开始的下标"), Category("自定义")]
  144. public virtual int StartIndex
  145. {
  146. get { return startIndex; }
  147. set
  148. {
  149. startIndex = value;
  150. if (startIndex <= 0)
  151. startIndex = 0;
  152. }
  153. }
  154. /// <summary>
  155. /// Initializes a new instance of the <see cref="UCPagerControlBase" /> class.
  156. /// </summary>
  157. public UCPagerControlBase()
  158. {
  159. InitializeComponent();
  160. }
  161. /// <summary>
  162. /// Handles the Load event of the UCPagerControlBase 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 UCPagerControlBase_Load(object sender, EventArgs e)
  167. {
  168. if (PageModel == HZH_Controls.Controls.PageModel.Soure)
  169. {
  170. if (DataSource == null)
  171. ShowBtn(false, false);
  172. else
  173. {
  174. ShowBtn(false, DataSource.Count > PageSize);
  175. }
  176. }
  177. else
  178. {
  179. if (PageCount <= 1)
  180. {
  181. ShowBtn(false, false);
  182. }
  183. else
  184. {
  185. ShowBtn(false, PageCount > 1);
  186. }
  187. }
  188. }
  189. /// <summary>
  190. /// 第一页
  191. /// </summary>
  192. public virtual void FirstPage()
  193. {
  194. if (PageModel == HZH_Controls.Controls.PageModel.Soure)
  195. {
  196. startIndex = 0;
  197. var s = GetCurrentSource();
  198. if (ShowSourceChanged != null)
  199. {
  200. ShowSourceChanged(s);
  201. }
  202. }
  203. else
  204. {
  205. startIndex = 0;
  206. PageIndex = 1;
  207. SetShow();
  208. if (ShowSourceChanged != null)
  209. {
  210. ShowSourceChanged(this);
  211. }
  212. }
  213. }
  214. /// <summary>
  215. /// 上一页
  216. /// </summary>
  217. public virtual void PreviousPage()
  218. {
  219. if (PageModel == HZH_Controls.Controls.PageModel.Soure)
  220. {
  221. if (startIndex == 0)
  222. return;
  223. startIndex -= m_pageSize;
  224. if (startIndex < 0)
  225. startIndex = 0;
  226. var s = GetCurrentSource();
  227. if (ShowSourceChanged != null)
  228. {
  229. ShowSourceChanged(s);
  230. }
  231. }
  232. else
  233. {
  234. if (PageIndex <= 1)
  235. return;
  236. PageIndex--;
  237. SetShow();
  238. if (ShowSourceChanged != null)
  239. {
  240. ShowSourceChanged(this);
  241. }
  242. }
  243. }
  244. /// <summary>
  245. /// 下一页
  246. /// </summary>
  247. public virtual void NextPage()
  248. {
  249. if (PageModel == HZH_Controls.Controls.PageModel.Soure)
  250. {
  251. if (startIndex + m_pageSize >= DataSource.Count)
  252. {
  253. return;
  254. }
  255. startIndex += m_pageSize;
  256. if (startIndex < 0)
  257. startIndex = 0;
  258. var s = GetCurrentSource();
  259. if (ShowSourceChanged != null)
  260. {
  261. ShowSourceChanged(s);
  262. }
  263. }
  264. else
  265. {
  266. if (PageIndex >= PageCount)
  267. return;
  268. PageIndex++;
  269. SetShow();
  270. if (ShowSourceChanged != null)
  271. {
  272. ShowSourceChanged(this);
  273. }
  274. }
  275. }
  276. /// <summary>
  277. /// 最后一页
  278. /// </summary>
  279. public virtual void EndPage()
  280. {
  281. if (PageModel == HZH_Controls.Controls.PageModel.Soure)
  282. {
  283. if (DataSource == null)
  284. {
  285. if (ShowSourceChanged != null)
  286. {
  287. ShowSourceChanged(null);
  288. }
  289. return;
  290. }
  291. startIndex = DataSource.Count - m_pageSize;
  292. if (startIndex < 0)
  293. startIndex = 0;
  294. var s = GetCurrentSource();
  295. if (ShowSourceChanged != null)
  296. {
  297. ShowSourceChanged(s);
  298. }
  299. }
  300. else
  301. {
  302. PageIndex = PageCount;
  303. SetShow();
  304. if (ShowSourceChanged != null)
  305. {
  306. ShowSourceChanged(this);
  307. }
  308. }
  309. }
  310. /// <summary>
  311. /// 刷新数据
  312. /// </summary>
  313. public virtual void Reload()
  314. {
  315. if (PageModel == HZH_Controls.Controls.PageModel.Soure)
  316. {
  317. var s = GetCurrentSource();
  318. if (ShowSourceChanged != null)
  319. {
  320. ShowSourceChanged(s);
  321. }
  322. }
  323. else
  324. {
  325. if (ShowSourceChanged != null)
  326. {
  327. ShowSourceChanged(this);
  328. }
  329. }
  330. }
  331. /// <summary>
  332. /// 获取当前页数据
  333. /// </summary>
  334. /// <returns>List&lt;System.Object&gt;.</returns>
  335. public virtual List<object> GetCurrentSource()
  336. {
  337. if (DataSource == null || DataSource.Count <= 0)
  338. return null;
  339. int intShowCount = m_pageSize;
  340. if (intShowCount + startIndex > DataSource.Count)
  341. intShowCount = DataSource.Count - startIndex;
  342. object[] objs = new object[intShowCount];
  343. DataSource.CopyTo(startIndex, objs, 0, intShowCount);
  344. var lst = objs.ToList();
  345. bool blnLeft = false;
  346. bool blnRight = false;
  347. if (lst.Count > 0)
  348. {
  349. if (DataSource.IndexOf(lst[0]) > 0)
  350. {
  351. blnLeft = true;
  352. }
  353. else
  354. {
  355. blnLeft = false;
  356. }
  357. if (DataSource.IndexOf(lst[lst.Count - 1]) >= DataSource.Count - 1)
  358. {
  359. blnRight = false;
  360. }
  361. else
  362. {
  363. blnRight = true;
  364. }
  365. }
  366. ShowBtn(blnLeft, blnRight);
  367. return lst;
  368. }
  369. /// <summary>
  370. /// 控制按钮显示
  371. /// </summary>
  372. /// <param name="blnLeftBtn">是否显示上一页,第一页</param>
  373. /// <param name="blnRightBtn">是否显示下一页,最后一页</param>
  374. protected virtual void ShowBtn(bool blnLeftBtn, bool blnRightBtn)
  375. { }
  376. private void SetShow()
  377. {
  378. bool blnLeft = false;
  379. bool blnRight = false;
  380. if (PageCount > 0)
  381. {
  382. if (PageIndex > 1)
  383. {
  384. blnLeft = true;
  385. }
  386. else
  387. {
  388. blnLeft = false;
  389. }
  390. if (PageIndex >= PageCount)
  391. {
  392. blnRight = false;
  393. }
  394. else
  395. {
  396. blnRight = true;
  397. }
  398. }
  399. ShowBtn(blnLeft, blnRight);
  400. }
  401. }
  402. }