UcCtrlPage.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using DevExpress.XtraEditors;
  2. using DevExpress.XtraPrinting;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace XdCxRhDW.App.UserControl
  13. {
  14. public partial class UcCtrlPage : DevExpress.XtraEditors.XtraUserControl
  15. {
  16. public event EventHandler OnPageChanged;
  17. public UcCtrlPage()
  18. {
  19. InitializeComponent();
  20. }
  21. //每页显示记录数
  22. private int pageSize = 10;
  23. //当前页
  24. private int pageIndex = 1;
  25. //总页数
  26. private int pageCount = 0;
  27. //总记录数
  28. private int totalCount = 0;
  29. public int PageSize { get => pageSize; set => pageSize = value; }
  30. public int PageIndex { get => pageIndex; set => pageIndex = value; }
  31. public int PageCount
  32. {
  33. get
  34. {
  35. if (pageSize != 0)
  36. {
  37. pageCount = GetPageCount();
  38. }
  39. return pageCount;
  40. }
  41. set { pageCount = value; }
  42. }
  43. public int TotalCount { get => totalCount; set => totalCount = value; }
  44. /// <summary>
  45. /// 计算总页数
  46. /// </summary>
  47. /// <returns></returns>
  48. private int GetPageCount()
  49. {
  50. if (PageSize == 0)
  51. {
  52. return 0;
  53. }
  54. int pageCount = TotalCount / PageSize;
  55. if (TotalCount % PageSize == 0)
  56. {
  57. pageCount = TotalCount / PageSize;
  58. }
  59. else
  60. {
  61. pageCount = TotalCount / PageSize + 1;
  62. }
  63. return pageCount;
  64. }
  65. /// <summary>
  66. /// 首页
  67. /// </summary>
  68. /// <param name="sender"></param>
  69. /// <param name="e"></param>
  70. private void linkFirst_LinkClicked(object sender, EventArgs e)
  71. {
  72. PageIndex = 1;
  73. DrawControl(true);
  74. }
  75. /// <summary>
  76. /// 上一页
  77. /// </summary>
  78. /// <param name="sender"></param>
  79. /// <param name="e"></param>
  80. private void linkPrev_LinkClicked(object sender, EventArgs e)
  81. {
  82. PageIndex = Math.Max(1, PageIndex - 1);
  83. DrawControl(true);
  84. }
  85. /// <summary>
  86. /// 下一页
  87. /// </summary>
  88. /// <param name="sender"></param>
  89. /// <param name="e"></param>
  90. private void linkNext_LinkClicked(object sender, EventArgs e)
  91. {
  92. PageIndex = Math.Min(PageCount, PageIndex + 1);
  93. DrawControl(true);
  94. }
  95. /// <summary>
  96. /// 末页
  97. /// </summary>
  98. /// <param name="sender"></param>
  99. /// <param name="e"></param>
  100. private void linkLast_LinkClicked(object sender, EventArgs e)
  101. {
  102. PageIndex = PageCount;
  103. DrawControl(true);
  104. }
  105. public void DrawControl(int count)
  106. {
  107. TotalCount = count;
  108. DrawControl(false);
  109. }
  110. /// <summary>
  111. /// 页面控件呈现
  112. /// </summary>
  113. private void DrawControl(bool callEvent)
  114. {
  115. lblTotalPage.Text = new StringBuilder("第 ").Append(PageIndex.ToString()).Append(" / ").Append(PageCount.ToString()).Append(" 页").ToString();
  116. lblTotalCount.Text = new StringBuilder("共 ").Append(TotalCount.ToString()).Append(" 条").ToString();
  117. cbbPageSize.Text = PageSize.ToString();
  118. if (callEvent && OnPageChanged != null)
  119. {
  120. OnPageChanged(this, null);//当前分页数字改变时,触发委托事件
  121. }
  122. SetFormCtrEnabled();
  123. if (PageCount <= 1)//有且仅有一页
  124. {
  125. linkFirst.Enabled = false;
  126. linkPrev.Enabled = false;
  127. linkNext.Enabled = false;
  128. linkLast.Enabled = false;
  129. linkGo.Enabled = false;
  130. }
  131. else if (PageIndex == 1)//第一页
  132. {
  133. linkFirst.Enabled = false;
  134. linkPrev.Enabled = false;
  135. }
  136. else if (PageIndex == PageCount)//最后一页
  137. {
  138. linkNext.Enabled = false;
  139. linkLast.Enabled = false;
  140. }
  141. }
  142. private void SetFormCtrEnabled()
  143. {
  144. linkFirst.Enabled = true;
  145. linkPrev.Enabled = true;
  146. linkNext.Enabled = true;
  147. linkLast.Enabled = true;
  148. linkGo.Enabled = true;
  149. }
  150. /// <summary>
  151. /// enter键功能
  152. /// </summary>
  153. private void txtPageNum_KeyPress(object sender, KeyPressEventArgs e)
  154. {
  155. linkGo_LinkClicked(null, null);
  156. }
  157. /// <summary>
  158. /// 跳转页数限制
  159. /// </summary>
  160. private void txtPageNum_TextChanged(object sender, EventArgs e)
  161. {
  162. int num = 0;
  163. if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
  164. {
  165. if (num > PageCount)
  166. {
  167. txtPageNum.Text = PageCount.ToString();
  168. }
  169. }
  170. }
  171. /// <summary>
  172. /// 跳转指定页
  173. /// </summary>
  174. /// <param name="sender"></param>
  175. /// <param name="e"></param>
  176. private void linkGo_LinkClicked(object sender, EventArgs e)
  177. {
  178. int num = 0;
  179. if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
  180. {
  181. PageIndex = num;
  182. DrawControl(true);
  183. }
  184. }
  185. private void PageControl_Paint(object sender, PaintEventArgs e)
  186. {
  187. drawBorder(e);
  188. }
  189. private void drawBorder(PaintEventArgs e)
  190. {
  191. Color _lineColor = Color.FromArgb(0, 123, 255);
  192. ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle,
  193. _lineColor, 1, ButtonBorderStyle.Solid, //左边
  194. _lineColor, 1, ButtonBorderStyle.Solid, //上边
  195. _lineColor, 1, ButtonBorderStyle.Solid, //右边
  196. _lineColor, 1, ButtonBorderStyle.Solid);//底边
  197. }
  198. /// <summary>
  199. /// 每页显示记录数更改事件
  200. /// </summary>
  201. bool isTextChanged = false;
  202. private void cbbPageSize_SelectedIndexChanged(object sender, EventArgs e)
  203. {
  204. int num = 0;
  205. if (!int.TryParse(cbbPageSize.Text.Trim(), out num) || num <= 0)
  206. {
  207. num = 10;
  208. cbbPageSize.Text = "10";
  209. }
  210. else
  211. {
  212. isTextChanged = true;
  213. }
  214. pageSize = num;
  215. if (isTextChanged)
  216. {
  217. isTextChanged = false;
  218. linkFirst_LinkClicked(null, null);
  219. }
  220. }
  221. }
  222. }