PagerControl.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Text.RegularExpressions;
  11. namespace WCS_Client.UC
  12. {
  13. public partial class PagerControl : UserControl
  14. {
  15. public PagerControl()
  16. {
  17. InitializeComponent();
  18. }
  19. private int record = 0;
  20. /// <summary>
  21. /// 总记录数
  22. /// </summary>
  23. public int Record
  24. {
  25. get { return record; }
  26. set
  27. {
  28. record = value;
  29. InitPageInfo();
  30. }
  31. }
  32. private int pageSize = 20;
  33. /// <summary>
  34. /// 每页条数
  35. /// </summary>
  36. public int PageSize
  37. {
  38. get { return pageSize; }
  39. set { pageSize = value; }
  40. }
  41. private int currentPage = 1;
  42. /// <summary>
  43. /// 当前页
  44. /// </summary>
  45. public int CurrentPage
  46. {
  47. get { return currentPage; }
  48. set { currentPage = value; }
  49. }
  50. public int pageNum = 0;
  51. /// <summary>
  52. /// 总页码
  53. /// </summary>
  54. public int PageNum
  55. {
  56. get
  57. {
  58. if (Record == 0)
  59. {
  60. pageNum = 0;
  61. }
  62. else
  63. {
  64. if (Record % PageSize > 0)
  65. {
  66. pageNum = Record / PageSize + 1;
  67. }
  68. else
  69. {
  70. pageNum = Record / PageSize;
  71. }
  72. }
  73. return pageNum;
  74. }
  75. }
  76. //定义委托
  77. public delegate void BindHandle(object sender, EventArgs e);
  78. /// <summary>
  79. /// 绑定数据源事件
  80. /// </summary>
  81. public event BindHandle BindSource;
  82. private void btnFirst_Click(object sender, EventArgs e)
  83. {
  84. if (Record > 0)
  85. {
  86. if (CurrentPage == 1)
  87. {
  88. MessageBox.Show("当前已经是首页");
  89. return;
  90. }
  91. else
  92. {
  93. CurrentPage = 1;
  94. if (BindSource != null)
  95. {
  96. BindSource(sender, e);
  97. InitPageInfo();
  98. }
  99. }
  100. }
  101. }
  102. private void btnPre_Click(object sender, EventArgs e)
  103. {
  104. if (Record > 0)
  105. {
  106. if (CurrentPage == 1)
  107. {
  108. MessageBox.Show("当前已经是首页");
  109. return;
  110. }
  111. else
  112. {
  113. CurrentPage = CurrentPage - 1;
  114. if (BindSource != null)
  115. {
  116. BindSource(sender, e);
  117. InitPageInfo();
  118. }
  119. }
  120. }
  121. }
  122. private void btnNext_Click(object sender, EventArgs e)
  123. {
  124. if (Record > 0)
  125. {
  126. if (CurrentPage == PageNum)
  127. {
  128. MessageBox.Show("当前已经是末页");
  129. return;
  130. }
  131. else
  132. {
  133. CurrentPage = CurrentPage + 1;
  134. if (BindSource != null)
  135. {
  136. BindSource(sender, e);
  137. InitPageInfo();
  138. }
  139. }
  140. }
  141. }
  142. private void btnLast_Click(object sender, EventArgs e)
  143. {
  144. if (Record > 0)
  145. {
  146. if (CurrentPage == PageNum)
  147. {
  148. MessageBox.Show("当前已经是末页");
  149. return;
  150. }
  151. else
  152. {
  153. CurrentPage = PageNum;
  154. if (BindSource != null)
  155. {
  156. BindSource(sender, e);
  157. InitPageInfo();
  158. }
  159. }
  160. }
  161. }
  162. private void InitPageInfo()
  163. {
  164. if (Record == 0 || (Record > 0 && CurrentPage > pageNum))
  165. {
  166. CurrentPage = 1;
  167. }
  168. lblInfo.Text = string.Format("共 {0} 条记录 共 {1} 页 当前第 {2} 页", Record, PageNum, CurrentPage);
  169. txtPage.Text = CurrentPage.ToString();
  170. }
  171. private void btnGo_Click(object sender, EventArgs e)
  172. {
  173. if (Record > 0)
  174. {
  175. if (!string.IsNullOrEmpty(txtPage.Text) && !Regex.IsMatch(txtPage.Text, @"^[\d]*$"))
  176. {
  177. MessageBox.Show("请正确填写页码!");
  178. return;
  179. }
  180. int page = Convert.ToInt32(txtPage.Text);
  181. if (page == 0)
  182. {
  183. page = 1;
  184. }
  185. if (page > PageNum)
  186. {
  187. page = PageNum;
  188. }
  189. CurrentPage = page;
  190. if (BindSource != null)
  191. {
  192. BindSource(sender, e);
  193. InitPageInfo();
  194. }
  195. }
  196. }
  197. private void PagerControl_Load(object sender, EventArgs e)
  198. {
  199. if (BindSource != null)
  200. {
  201. BindSource(sender, e);
  202. InitPageInfo();
  203. }
  204. }
  205. }
  206. }