123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Text.RegularExpressions;
- namespace WCS_Client.UC
- {
- public partial class PagerControl : UserControl
- {
- public PagerControl()
- {
- InitializeComponent();
- }
- private int record = 0;
- /// <summary>
- /// 总记录数
- /// </summary>
- public int Record
- {
- get { return record; }
- set
- {
- record = value;
- InitPageInfo();
- }
- }
- private int pageSize = 20;
- /// <summary>
- /// 每页条数
- /// </summary>
- public int PageSize
- {
- get { return pageSize; }
- set { pageSize = value; }
- }
- private int currentPage = 1;
- /// <summary>
- /// 当前页
- /// </summary>
- public int CurrentPage
- {
- get { return currentPage; }
- set { currentPage = value; }
- }
- public int pageNum = 0;
- /// <summary>
- /// 总页码
- /// </summary>
- public int PageNum
- {
- get
- {
- if (Record == 0)
- {
- pageNum = 0;
- }
- else
- {
- if (Record % PageSize > 0)
- {
- pageNum = Record / PageSize + 1;
- }
- else
- {
- pageNum = Record / PageSize;
- }
- }
- return pageNum;
- }
- }
- //定义委托
- public delegate void BindHandle(object sender, EventArgs e);
- /// <summary>
- /// 绑定数据源事件
- /// </summary>
- public event BindHandle BindSource;
- private void btnFirst_Click(object sender, EventArgs e)
- {
- if (Record > 0)
- {
- if (CurrentPage == 1)
- {
- MessageBox.Show("当前已经是首页");
- return;
- }
- else
- {
- CurrentPage = 1;
- if (BindSource != null)
- {
- BindSource(sender, e);
- InitPageInfo();
- }
- }
- }
- }
- private void btnPre_Click(object sender, EventArgs e)
- {
- if (Record > 0)
- {
- if (CurrentPage == 1)
- {
- MessageBox.Show("当前已经是首页");
- return;
- }
- else
- {
- CurrentPage = CurrentPage - 1;
- if (BindSource != null)
- {
- BindSource(sender, e);
- InitPageInfo();
- }
- }
- }
- }
- private void btnNext_Click(object sender, EventArgs e)
- {
- if (Record > 0)
- {
- if (CurrentPage == PageNum)
- {
- MessageBox.Show("当前已经是末页");
- return;
- }
- else
- {
- CurrentPage = CurrentPage + 1;
- if (BindSource != null)
- {
- BindSource(sender, e);
- InitPageInfo();
- }
- }
- }
- }
- private void btnLast_Click(object sender, EventArgs e)
- {
- if (Record > 0)
- {
- if (CurrentPage == PageNum)
- {
- MessageBox.Show("当前已经是末页");
- return;
- }
- else
- {
- CurrentPage = PageNum;
- if (BindSource != null)
- {
- BindSource(sender, e);
- InitPageInfo();
- }
- }
- }
- }
- private void InitPageInfo()
- {
- if (Record == 0 || (Record > 0 && CurrentPage > pageNum))
- {
- CurrentPage = 1;
- }
- lblInfo.Text = string.Format("共 {0} 条记录 共 {1} 页 当前第 {2} 页", Record, PageNum, CurrentPage);
- txtPage.Text = CurrentPage.ToString();
- }
- private void btnGo_Click(object sender, EventArgs e)
- {
- if (Record > 0)
- {
- if (!string.IsNullOrEmpty(txtPage.Text) && !Regex.IsMatch(txtPage.Text, @"^[\d]*$"))
- {
- MessageBox.Show("请正确填写页码!");
- return;
- }
- int page = Convert.ToInt32(txtPage.Text);
- if (page == 0)
- {
- page = 1;
- }
- if (page > PageNum)
- {
- page = PageNum;
- }
- CurrentPage = page;
- if (BindSource != null)
- {
- BindSource(sender, e);
- InitPageInfo();
- }
- }
- }
- private void PagerControl_Load(object sender, EventArgs e)
- {
- if (BindSource != null)
- {
- BindSource(sender, e);
- InitPageInfo();
- }
- }
- }
- }
|