版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

264 wiersze
7.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace MaiMuAOI.SysUI.DefectPicShow
  11. {
  12. public partial class PageCtrl : UserControl
  13. {
  14. public PageCtrl()
  15. {
  16. InitializeComponent();
  17. numPageDataCount.Controls[0].Visible = false;
  18. numGoto.Controls[0].Visible = false;
  19. }
  20. #region 分页字段和属性
  21. private int pageIndex = 1;
  22. /// <summary>
  23. /// 当前页数
  24. /// </summary>
  25. public virtual int PageIndex
  26. {
  27. get { return pageIndex; }
  28. set { pageIndex = value; }
  29. }
  30. private int pageSize = 10;
  31. /// <summary>
  32. /// 每页记录数
  33. /// </summary>
  34. public virtual int PageSize
  35. {
  36. get { return pageSize; }
  37. set { pageSize = value; }
  38. }
  39. private int recordCount = 0;
  40. /// <summary>
  41. /// 总记录数
  42. /// </summary>
  43. public virtual int RecordCount
  44. {
  45. get { return recordCount; }
  46. set { recordCount = value; }
  47. }
  48. private int pageCount = 0;
  49. /// <summary>
  50. /// 总页数
  51. /// </summary>
  52. public int PageCount
  53. {
  54. get
  55. {
  56. if (pageSize != 0)
  57. {
  58. pageCount = GetPageCount();
  59. }
  60. return pageCount;
  61. }
  62. }
  63. #endregion
  64. #region 页码变化时触发事件
  65. public event EventHandler OnPageChanged;
  66. #endregion
  67. #region 分页及相关事件功能实现
  68. /// <summary>
  69. /// 设窗体控件全部可用
  70. /// </summary>
  71. private void SetFormCtrEnabled()
  72. {
  73. linkFirst.Enabled = true;
  74. linkPrevious.Enabled = true;
  75. linkNext.Enabled = true;
  76. linkLast.Enabled = true;
  77. btnGo.Enabled = true;
  78. }
  79. /// <summary>
  80. /// 计算总页数
  81. /// </summary>
  82. /// <returns></returns>
  83. private int GetPageCount()
  84. {
  85. if (PageSize == 0)
  86. {
  87. return 0;
  88. }
  89. int pageCount = RecordCount / PageSize;
  90. if (RecordCount % PageSize == 0)
  91. {
  92. pageCount = RecordCount / PageSize;
  93. }
  94. else
  95. {
  96. pageCount = RecordCount / PageSize + 1;
  97. }
  98. return pageCount;
  99. }
  100. /// <summary>
  101. /// 用于客户端调用
  102. /// </summary>
  103. public void DrawControl(int count)
  104. {
  105. recordCount = count;
  106. DrawControl(false);
  107. }
  108. /// <summary>
  109. /// 根据不同的条件,改变页面控件的呈现状态
  110. /// </summary>
  111. private void DrawControl(bool callEvent)
  112. {
  113. lblCurrentPage.Text = PageIndex.ToString();
  114. lblPageCount.Text = PageCount.ToString();
  115. lblTotalCount.Text = RecordCount.ToString();
  116. numPageDataCount.Value = PageSize;
  117. if (callEvent && OnPageChanged != null)
  118. {
  119. OnPageChanged(this, null);//当前分页数字改变时,触发委托事件
  120. }
  121. SetFormCtrEnabled();
  122. if (PageCount == 1)//有且仅有一页时
  123. {
  124. linkFirst.Enabled = false;
  125. linkPrevious.Enabled = false;
  126. linkNext.Enabled = false;
  127. linkLast.Enabled = false;
  128. btnGo.Enabled = false;
  129. }
  130. else if (PageIndex == 1)//当前页为第一页时
  131. {
  132. linkFirst.Enabled = false;
  133. linkPrevious.Enabled = false;
  134. }
  135. else if (PageIndex == PageCount)//当前页为最后一页时
  136. {
  137. linkNext.Enabled = false;
  138. linkLast.Enabled = false;
  139. }
  140. }
  141. #endregion
  142. #region 相关控件事件
  143. //首页按钮
  144. private void linkFirst_Click(object sender, EventArgs e)
  145. {
  146. PageIndex = 1;
  147. DrawControl(true);
  148. }
  149. //上一页按钮
  150. private void linkPrevious_Click(object sender, EventArgs e)
  151. {
  152. PageIndex = Math.Max(1, PageIndex - 1);
  153. DrawControl(true);
  154. }
  155. //下一页按钮
  156. private void linkNext_Click(object sender, EventArgs e)
  157. {
  158. PageIndex = Math.Min(PageCount, PageIndex + 1);
  159. DrawControl(true);
  160. }
  161. //尾页按钮
  162. private void linkLast_Click(object sender, EventArgs e)
  163. {
  164. PageIndex = PageCount;
  165. DrawControl(true);
  166. }
  167. /// <summary>
  168. /// 按下enter键,执行跳转页面功能
  169. /// </summary>
  170. private void txtPageNum_KeyPress(object sender, KeyPressEventArgs e)
  171. {
  172. btnGo_Click(null, null);
  173. }
  174. /// <summary>
  175. /// 跳转页数限制
  176. /// </summary>
  177. private void txtPageNum_TextChanged(object sender, EventArgs e)
  178. {
  179. int num = 0;
  180. if (int.TryParse(numGoto.Value.ToString(), out num) && num > 0)
  181. { //TryParse 函数,将字符串转换成等效的整数,返回bool型,判断是否转换成功。
  182. //输入除数字以外的字符是转换不成功的
  183. if (num > PageCount) //输入数量大于最大页数时,文本框自动显示最大页数
  184. {
  185. numGoto.Value = PageCount;
  186. }
  187. }
  188. }
  189. /// <summary>
  190. /// 跳转按钮
  191. /// </summary>
  192. /// <param name="sender"></param>
  193. /// <param name="e"></param>
  194. private void btnGo_Click(object sender, EventArgs e)
  195. {
  196. int num = 0;
  197. if (int.TryParse(numGoto.Value.ToString(), out num) && num > 0)
  198. {
  199. PageIndex = num;
  200. DrawControl(true);
  201. }
  202. }
  203. #endregion
  204. bool isTextChanged = false;
  205. /// <summary>
  206. /// 每页显示的记录数改变时
  207. /// </summary>
  208. private void txtPageSize_TextChanged(object sender, EventArgs e)
  209. {
  210. int num = 0;
  211. //输入不符合规范时,默认设置为100
  212. if (!int.TryParse(numPageDataCount.Text.Trim(), out num) || num <= 0)
  213. {
  214. num = 100;
  215. numPageDataCount.Value = 100;
  216. }
  217. else
  218. {
  219. isTextChanged = true;
  220. }
  221. pageSize = num;
  222. }
  223. /// <summary>
  224. /// 光标离开 每页设置文本框时,显示到首页
  225. private void txtPageSize_Leave(object sender, EventArgs e)
  226. {
  227. if (isTextChanged)
  228. {
  229. isTextChanged = false;
  230. linkFirst_Click(null, null);
  231. }
  232. }
  233. }
  234. }