UCPagerControl2.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-15-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCPagerControl2.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 UCPagerControl2.
  28. /// Implements the <see cref="HZH_Controls.Controls.UCPagerControlBase" />
  29. /// </summary>
  30. /// <seealso cref="HZH_Controls.Controls.UCPagerControlBase" />
  31. [ToolboxItem(true)]
  32. public partial class UCPagerControl2 : UCPagerControlBase
  33. {
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="UCPagerControl2" /> class.
  36. /// </summary>
  37. public UCPagerControl2()
  38. {
  39. InitializeComponent();
  40. txtPage.txtInput.KeyDown += txtInput_KeyDown;
  41. }
  42. /// <summary>
  43. /// Handles the KeyDown event of the txtInput control.
  44. /// </summary>
  45. /// <param name="sender">The source of the event.</param>
  46. /// <param name="e">The <see cref="KeyEventArgs" /> instance containing the event data.</param>
  47. void txtInput_KeyDown(object sender, KeyEventArgs e)
  48. {
  49. if (e.KeyCode == Keys.Enter)
  50. {
  51. btnToPage_BtnClick(null, null);
  52. txtPage.InputText = "";
  53. }
  54. }
  55. /// <summary>
  56. /// Occurs when [show source changed].
  57. /// </summary>
  58. public override event PageControlEventHandler ShowSourceChanged;
  59. /// <summary>
  60. /// 关联的数据源
  61. /// </summary>
  62. /// <value>The data source.</value>
  63. public override List<object> DataSource
  64. {
  65. get
  66. {
  67. return base.DataSource;
  68. }
  69. set
  70. {
  71. if (value == null)
  72. {
  73. base.DataSource = new List<object>();
  74. }
  75. else
  76. {
  77. base.DataSource = value;
  78. }
  79. PageIndex = 1;
  80. ResetPageCount();
  81. var s = GetCurrentSource();
  82. if (ShowSourceChanged != null)
  83. {
  84. ShowSourceChanged(s);
  85. }
  86. }
  87. }
  88. /// <summary>
  89. /// 每页显示数量
  90. /// </summary>
  91. /// <value>The size of the page.</value>
  92. public override int PageSize
  93. {
  94. get
  95. {
  96. return base.PageSize;
  97. }
  98. set
  99. {
  100. base.PageSize = value;
  101. if (PageModel == HZH_Controls.Controls.PageModel.Soure)
  102. {
  103. ResetPageCount();
  104. var s = GetCurrentSource();
  105. if (ShowSourceChanged != null)
  106. {
  107. ShowSourceChanged(s);
  108. }
  109. }
  110. else
  111. {
  112. if (ShowSourceChanged != null)
  113. {
  114. ShowSourceChanged(this);
  115. }
  116. }
  117. }
  118. }
  119. public override int PageCount
  120. {
  121. get
  122. {
  123. return base.PageCount;
  124. }
  125. set
  126. {
  127. base.PageCount = value;
  128. ReloadPage();
  129. }
  130. }
  131. public override int PageIndex
  132. {
  133. get
  134. {
  135. return base.PageIndex;
  136. }
  137. set
  138. {
  139. base.PageIndex = value;
  140. ReloadPage();
  141. }
  142. }
  143. /// <summary>
  144. /// 第一页
  145. /// </summary>
  146. public override void FirstPage()
  147. {
  148. if (PageIndex == 1)
  149. return;
  150. PageIndex = 1;
  151. ReloadPage();
  152. if (PageModel == HZH_Controls.Controls.PageModel.Soure)
  153. {
  154. StartIndex = (PageIndex - 1) * PageSize;
  155. var s = GetCurrentSource();
  156. if (ShowSourceChanged != null)
  157. {
  158. ShowSourceChanged(s);
  159. }
  160. }
  161. else
  162. {
  163. if (ShowSourceChanged != null)
  164. {
  165. ShowSourceChanged(this);
  166. }
  167. }
  168. }
  169. /// <summary>
  170. /// 上一页
  171. /// </summary>
  172. public override void PreviousPage()
  173. {
  174. if (PageIndex <= 1)
  175. {
  176. return;
  177. }
  178. PageIndex--;
  179. ReloadPage();
  180. if (PageModel == HZH_Controls.Controls.PageModel.Soure)
  181. {
  182. StartIndex = (PageIndex - 1) * PageSize;
  183. var s = GetCurrentSource();
  184. if (ShowSourceChanged != null)
  185. {
  186. ShowSourceChanged(s);
  187. }
  188. }
  189. else
  190. {
  191. if (ShowSourceChanged != null)
  192. {
  193. ShowSourceChanged(this);
  194. }
  195. }
  196. }
  197. /// <summary>
  198. /// 下一页
  199. /// </summary>
  200. public override void NextPage()
  201. {
  202. if (PageIndex >= PageCount)
  203. {
  204. return;
  205. }
  206. PageIndex++;
  207. ReloadPage();
  208. if (PageModel == HZH_Controls.Controls.PageModel.Soure)
  209. {
  210. StartIndex = (PageIndex - 1) * PageSize;
  211. var s = GetCurrentSource();
  212. if (ShowSourceChanged != null)
  213. {
  214. ShowSourceChanged(s);
  215. }
  216. }
  217. else
  218. {
  219. if (ShowSourceChanged != null)
  220. {
  221. ShowSourceChanged(this);
  222. }
  223. }
  224. }
  225. /// <summary>
  226. /// 最后一页
  227. /// </summary>
  228. public override void EndPage()
  229. {
  230. if (PageIndex == PageCount)
  231. return;
  232. PageIndex = PageCount;
  233. ReloadPage();
  234. if (PageModel == HZH_Controls.Controls.PageModel.Soure)
  235. {
  236. StartIndex = (PageIndex - 1) * PageSize;
  237. var s = GetCurrentSource();
  238. if (ShowSourceChanged != null)
  239. {
  240. ShowSourceChanged(s);
  241. }
  242. }
  243. else
  244. {
  245. if (ShowSourceChanged != null)
  246. {
  247. ShowSourceChanged(this);
  248. }
  249. }
  250. }
  251. /// <summary>
  252. /// Resets the page count.
  253. /// </summary>
  254. private void ResetPageCount()
  255. {
  256. if (PageSize > 0)
  257. {
  258. if (base.DataSource != null)
  259. PageCount = base.DataSource.Count / base.PageSize + (base.DataSource.Count % base.PageSize > 0 ? 1 : 0);
  260. }
  261. txtPage.MaxValue = PageCount;
  262. txtPage.MinValue = 1;
  263. ReloadPage();
  264. }
  265. /// <summary>
  266. /// Reloads the page.
  267. /// </summary>
  268. private void ReloadPage()
  269. {
  270. try
  271. {
  272. ControlHelper.FreezeControl(tableLayoutPanel1, true);
  273. List<int> lst = new List<int>();
  274. if (PageCount <= 9)
  275. {
  276. for (var i = 1; i <= PageCount; i++)
  277. {
  278. lst.Add(i);
  279. }
  280. }
  281. else
  282. {
  283. if (this.PageIndex <= 6)
  284. {
  285. for (var i = 1; i <= 7; i++)
  286. {
  287. lst.Add(i);
  288. }
  289. lst.Add(-1);
  290. lst.Add(PageCount);
  291. }
  292. else if (this.PageIndex > PageCount - 6)
  293. {
  294. lst.Add(1);
  295. lst.Add(-1);
  296. for (var i = PageCount - 6; i <= PageCount; i++)
  297. {
  298. lst.Add(i);
  299. }
  300. }
  301. else
  302. {
  303. lst.Add(1);
  304. lst.Add(-1);
  305. var begin = PageIndex - 2;
  306. var end = PageIndex + 2;
  307. if (end > PageCount)
  308. {
  309. end = PageCount;
  310. begin = end - 4;
  311. if (PageIndex - begin < 2)
  312. {
  313. begin = begin - 1;
  314. }
  315. }
  316. else if (end + 1 == PageCount)
  317. {
  318. end = PageCount;
  319. }
  320. for (var i = begin; i <= end; i++)
  321. {
  322. lst.Add(i);
  323. }
  324. if (end != PageCount)
  325. {
  326. lst.Add(-1);
  327. lst.Add(PageCount);
  328. }
  329. }
  330. }
  331. for (int i = 0; i < 9; i++)
  332. {
  333. UCBtnExt c = (UCBtnExt)this.tableLayoutPanel1.Controls.Find("p" + (i + 1), false)[0];
  334. if (i >= lst.Count)
  335. {
  336. c.Visible = false;
  337. }
  338. else
  339. {
  340. if (lst[i] == -1)
  341. {
  342. c.BtnText = "...";
  343. c.Enabled = false;
  344. }
  345. else
  346. {
  347. c.BtnText = lst[i].ToString();
  348. c.Enabled = true;
  349. }
  350. c.Visible = true;
  351. if (lst[i] == PageIndex)
  352. {
  353. c.RectColor = Color.FromArgb(255, 77, 59);
  354. }
  355. else
  356. {
  357. c.RectColor = Color.FromArgb(223, 223, 223);
  358. }
  359. }
  360. }
  361. ShowBtn(PageIndex > 1, PageIndex < PageCount);
  362. }
  363. finally
  364. {
  365. ControlHelper.FreezeControl(tableLayoutPanel1, false);
  366. }
  367. }
  368. /// <summary>
  369. /// Handles the BtnClick event of the page control.
  370. /// </summary>
  371. /// <param name="sender">The source of the event.</param>
  372. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  373. private void page_BtnClick(object sender, EventArgs e)
  374. {
  375. PageIndex = (sender as UCBtnExt).BtnText.ToInt();
  376. StartIndex = (PageIndex - 1) * PageSize;
  377. ReloadPage();
  378. if (PageModel == HZH_Controls.Controls.PageModel.Soure)
  379. {
  380. var s = GetCurrentSource();
  381. if (ShowSourceChanged != null)
  382. {
  383. ShowSourceChanged(s);
  384. }
  385. }
  386. else
  387. {
  388. if (ShowSourceChanged != null)
  389. {
  390. ShowSourceChanged(this);
  391. }
  392. }
  393. }
  394. /// <summary>
  395. /// 控制按钮显示
  396. /// </summary>
  397. /// <param name="blnLeftBtn">是否显示上一页,第一页</param>
  398. /// <param name="blnRightBtn">是否显示下一页,最后一页</param>
  399. protected override void ShowBtn(bool blnLeftBtn, bool blnRightBtn)
  400. {
  401. btnFirst.Enabled = btnPrevious.Enabled = blnLeftBtn;
  402. btnNext.Enabled = btnEnd.Enabled = blnRightBtn;
  403. }
  404. /// <summary>
  405. /// Handles the BtnClick event of the btnFirst control.
  406. /// </summary>
  407. /// <param name="sender">The source of the event.</param>
  408. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  409. private void btnFirst_BtnClick(object sender, EventArgs e)
  410. {
  411. FirstPage();
  412. }
  413. /// <summary>
  414. /// Handles the BtnClick event of the btnPrevious control.
  415. /// </summary>
  416. /// <param name="sender">The source of the event.</param>
  417. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  418. private void btnPrevious_BtnClick(object sender, EventArgs e)
  419. {
  420. PreviousPage();
  421. }
  422. /// <summary>
  423. /// Handles the BtnClick event of the btnNext control.
  424. /// </summary>
  425. /// <param name="sender">The source of the event.</param>
  426. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  427. private void btnNext_BtnClick(object sender, EventArgs e)
  428. {
  429. NextPage();
  430. }
  431. /// <summary>
  432. /// Handles the BtnClick event of the btnEnd control.
  433. /// </summary>
  434. /// <param name="sender">The source of the event.</param>
  435. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  436. private void btnEnd_BtnClick(object sender, EventArgs e)
  437. {
  438. EndPage();
  439. }
  440. /// <summary>
  441. /// Handles the BtnClick event of the btnToPage control.
  442. /// </summary>
  443. /// <param name="sender">The source of the event.</param>
  444. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  445. private void btnToPage_BtnClick(object sender, EventArgs e)
  446. {
  447. if (!string.IsNullOrEmpty(txtPage.InputText))
  448. {
  449. PageIndex = txtPage.InputText.ToInt();
  450. StartIndex = (PageIndex - 1) * PageSize;
  451. ReloadPage();
  452. if (PageModel == HZH_Controls.Controls.PageModel.Soure)
  453. {
  454. var s = GetCurrentSource();
  455. if (ShowSourceChanged != null)
  456. {
  457. ShowSourceChanged(s);
  458. }
  459. }
  460. else
  461. {
  462. if (ShowSourceChanged != null)
  463. {
  464. ShowSourceChanged(this);
  465. }
  466. }
  467. }
  468. }
  469. }
  470. }