using System; using System.Windows.Forms; namespace HL_FristAidPlatform_Public { public partial class UserControlForPage : UserControl { #region 变量 /// /// 总条数 /// private int totalNumber = 0; /// /// 每页条数 /// private int pageSize = 10; /// /// 当前页 /// private int curPage = 1; /// /// 委托 /// /// /// public delegate void MyPagerEvents(int curPage, int pageSize); /// /// 委托 /// /// /// public delegate void ExportEvents(bool singlePage);//单页,所有 /// /// 基于上面的委托定义事件 /// public event MyPagerEvents myPagerEvents; /// /// 基于上面的委托定义事件 /// public event ExportEvents exportEvents; #endregion 变量 public UserControlForPage() { InitializeComponent(); } /// /// 计算分页,分页大小,总记录数。 /// /// /// /// public void RefreshPager(int pageSize, int totalNumber, int curPage) { try { this.totalNumber = totalNumber; this.pageSize = pageSize; this.curPage = curPage; lcStatus.Text = string.Format("(共{0}条记录,每页{1}条,共{2}页)", totalNumber, pageSize, GetPageCount()); textEditToPage.Text = curPage.ToString(); comboBoxEditPageSize.Text = pageSize.ToString(); if (curPage == 0) { if (GetPageCount() > 0 && myPagerEvents != null) { curPage = 1; myPagerEvents(curPage, pageSize); } } if (curPage > GetPageCount() && myPagerEvents != null) { curPage = GetPageCount(); myPagerEvents(curPage, pageSize); } } catch (Exception ex) { PublicClass.WriteErrorLog("分页控件", "RefreshPager:\r\n" + ex); } } /// /// 获取总记录数 /// /// public int GetTotalNumber() { return totalNumber; } /// /// 获得当前页编号,从1开始 /// /// public int GetCurPage() { return curPage; } /// /// 获得总页数 /// /// public int GetPageCount() { try { int count = 0; if (totalNumber % pageSize == 0) { count = totalNumber / pageSize; } else { count = totalNumber / pageSize + 1; } if (count == 0) { count = 1; } return count; } catch (Exception ex) { PublicClass.WriteErrorLog("分页控件", "获得总页数:\r\n" + ex); return 0; } } /// /// 下一页 /// /// /// private void simpleButtonNext_Click(object sender, EventArgs e) { try { if (myPagerEvents != null) { if (curPage < GetPageCount()) curPage += 1; myPagerEvents(curPage, pageSize); } } catch (Exception ex) { PublicClass.WriteErrorLog("分页控件", "下一页:\r\n" + ex); } } /// /// 最后一页 /// /// /// private void simpleButtonEnd_Click(object sender, EventArgs e) { try { if (myPagerEvents != null) { curPage = GetPageCount(); myPagerEvents(curPage, pageSize); } } catch (Exception ex) { PublicClass.WriteErrorLog("分页控件", "最后一页:\r\n" + ex); } } /// /// 上一页 /// /// /// private void simpleButtonPre_Click(object sender, EventArgs e) { try { if (myPagerEvents != null) { if (curPage > 1) curPage -= 1; myPagerEvents(curPage, pageSize); } } catch (Exception ex) { PublicClass.WriteErrorLog("分页控件", "上一页:\r\n" + ex); } } /// /// 首页 /// /// /// private void simpleButtonFirst_Click(object sender, EventArgs e) { try { if (myPagerEvents != null) { curPage = 1; myPagerEvents(curPage, pageSize); } } catch (Exception ex) { PublicClass.WriteErrorLog("分页控件", "首页:\r\n" + ex); } } /// /// 跳转 /// /// /// private void simpleButtonToPage_Click(object sender, EventArgs e) { try { int selPage = Convert.ToInt32(textEditToPage.Text); if (myPagerEvents != null) { if ((selPage >= 1) && (selPage <= GetPageCount())) curPage = selPage; myPagerEvents(curPage, pageSize); } } catch (Exception ex) { PublicClass.WriteErrorLog("分页控件", "跳转:\r\n" + ex); } } /// /// 到处当前页 /// /// /// private void simpleButtonExportCurPage_Click(object sender, EventArgs e) { try { if (exportEvents != null) { exportEvents(true); } } catch (Exception ex) { PublicClass.WriteErrorLog("分页控件", "到处当前页:\r\n" + ex); } } /// /// 导出全部页 /// /// /// private void simpleButtonExportAllPage_Click(object sender, EventArgs e) { try { exportEvents?.Invoke(false); } catch (Exception ex) { PublicClass.WriteErrorLog("分页控件", "导出全部页:\r\n" + ex); } } /// /// 每页大小 /// /// /// private void comboBoxEditPageSize_EditValueChanged(object sender, EventArgs e) { try { int pageSize = Convert.ToInt32(comboBoxEditPageSize.Text); if ((pageSize > 0) && myPagerEvents != null) { this.pageSize = pageSize; myPagerEvents(curPage, pageSize); } } catch (Exception ex) { PublicClass.WriteErrorLog("分页控件", "每页大小:\r\n" + ex); } } } }