123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace LYFZ.ComponentLibrary
- {
- public partial class PagerEx : UserControl
- {
- /// 声明委托
- /// </summary>
- /// <param name="e"></param>
- public delegate void EventPagingHandler(EventArgs e);
- /// <summary>
- /// 构造函数
- /// </summary>
- public PagerEx()
- {
- InitializeComponent();
-
- }
- public event EventPagingHandler EventPaging;
- #region 公开属性
- private int _pageSize;
- /// <summary>
- /// 每页显示记录数(默认10)
- /// </summary>
- public int PageSize
- {
- get
- {
- return _pageSize;
- }
- set
- {
- if (value > 0)
- {
- _pageSize = value;
- }
- else
- {
- _pageSize = 50;
- }
- this.comboPageSize.Text = _pageSize.ToString();
- }
- }
- private int _currentPage = 1;
- /// <summary>
- /// 当前页
- /// </summary>
- public int CurrentPage
- {
- get
- {
- return _currentPage;
- }
- set
- {
- if (value > 0)
- {
- _currentPage = value;
- }
- else
- {
- _currentPage = 1;
- }
- }
- }
- private int _totalCount = 0;
- /// <summary>
- /// 总记录数
- /// </summary>
- public int TotalCount
- {
- get
- {
- return _totalCount;
- }
- set
- {
- if (value >= 0)
- {
- _totalCount = value;
- }
- else
- {
- _totalCount = 0;
- }
- this.lblTotalCount.Text = this._totalCount.ToString();
- CalculatePageCount();
- }
- }
- private int _pageCount = 0;
- /// <summary>
- /// 页数
- /// </summary>
- public int PageCount
- {
- get
- {
- return _pageCount;
- }
- set
- {
- if (value >= 0)
- {
- _pageCount = value;
- }
- else
- {
- _pageCount = 0;
- }
- this.lblPageCount.Text = _pageCount + "";
- }
- }
- #endregion
- #region 算法与绑定
- /// <summary>
- /// 计算页数
- /// </summary>
- private void CalculatePageCount()
- {
- //总条数大于0
- if (this.TotalCount > 0)
- {
- this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.TotalCount) / Convert.ToDouble(this.PageSize)));
- if (this.CurrentPage > this.PageCount)
- {
- this.CurrentPage = this.PageCount;
- }
- this.txtBoxCurPage.Text = this.CurrentPage + "";
- this.lblTotalCount.Text = this.TotalCount + "";
- this.lblPageCount.Text = this.PageCount + "";
- if (this.CurrentPage == 1)
- {
- this.btnFirst.Enabled = false;
- this.btnPrev.Enabled = false;
- this.lbtnGO.Enabled = true;
- }
- else
- {
- this.btnFirst.Enabled = true;
- this.btnPrev.Enabled = true;
- }
- if (this.CurrentPage == this.PageCount)
- {
- this.btnNext.Enabled = false;
- this.btnLast.Enabled = false;
- this.lbtnGO.Enabled = true;
- }
- else
- {
- this.btnLast.Enabled = true;
- this.btnNext.Enabled = true;
- }
- //总数等于0
- if (this.TotalCount == 0)
- {
- this.btnFirst.Enabled = false;
- this.btnPrev.Enabled = false;
- this.btnNext.Enabled = false;
- this.btnLast.Enabled = false;
- this.lbtnGO.Enabled = false;
- this.txtBoxCurPage.Text = "";
- }
- if (this.CurrentPage < this.PageCount)
- {
- this.btnNext.Enabled = true;
- }
- else { this.btnNext.Enabled = false; }
- }//总条数为空
- else
- {
- this.PageCount = 0;
- this.btnFirst.Enabled = false;
- this.btnPrev.Enabled = false;
- this.btnNext.Enabled = false;
- this.btnLast.Enabled = false;
- this.lbtnGO.Enabled = false;
- this.txtBoxCurPage.Text = "";
- }
- }
-
- /// <summary>
- /// 数据绑定
- /// </summary>
- public void Bind()
- {
- if (this.EventPaging != null)
- {
- this.EventPaging(new EventArgs());
- }
- if (this.CurrentPage > this.PageCount)
- {
- this.CurrentPage = this.PageCount;
- }
- this.txtBoxCurPage.Text = this.CurrentPage + "";
- this.lblTotalCount.Text = this.TotalCount + "";
- this.lblPageCount.Text = this.PageCount + "";
- if (this.CurrentPage == 1)
- {
- this.btnFirst.Enabled = false;
- this.btnPrev.Enabled = false;
- }
- else
- {
- this.btnFirst.Enabled = true;
- this.btnPrev.Enabled = true;
- }
- if (this.CurrentPage == this.PageCount)
- {
- this.btnNext.Enabled = false;
- this.btnLast.Enabled = false;
- this.lbtnGO.Enabled = true;
- }
- else
- {
- this.btnLast.Enabled = true;
- this.btnNext.Enabled = true;
- }
- //总数等于0
- if (this.TotalCount == 0)
- {
- this.btnFirst.Enabled = false;
- this.btnPrev.Enabled = false;
- this.btnNext.Enabled = false;
- this.btnLast.Enabled = false;
- this.lbtnGO.Enabled = false;
- this.txtBoxCurPage.Text = "";
- }
- if (this.CurrentPage < this.PageCount)
- {
- this.btnNext.Enabled = true;
- }
- else { this.btnNext.Enabled = false; }
- }
- #endregion
- #region 按钮事件
- private void btnFirst_Click(object sender, EventArgs e)
- {
- this.CurrentPage = 1;
- this.Bind();
- }
- private void btnPrev_Click(object sender, EventArgs e)
- {
- this.CurrentPage -= 1;
- this.Bind();
- }
- private void btnNext_Click(object sender, EventArgs e)
- {
- this.CurrentPage += 1;
- this.Bind();
- }
- private void btnLast_Click(object sender, EventArgs e)
- {
- this.CurrentPage = this.PageCount;
- this.Bind();
- }
- private void lbtnGO_Click(object sender, EventArgs e)
- {
- try
- {
- Convert.ToInt32(txtBoxCurPage.Text);
- if (Convert.ToInt32(txtBoxCurPage.Text) > PageCount || Convert.ToInt32(txtBoxCurPage.Text) == PageCount)
- {
- this.txtBoxCurPage.Text = this.PageCount.ToString();
- this.CurrentPage = PageCount;
- this.Bind();
- }
- else
- {
- this.CurrentPage = Convert.ToInt32(txtBoxCurPage.Text);
- this.Bind();
- }
- }
- catch
- {
- MessageBoxCustom.Show("请输入数字");
- }
- }
- private void txtBoxCurPage_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (!Char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar))
- {
- MessageBoxCustom.Show("请输入数字");
- e.Handled = true;
- }
- }
- #endregion
- }
- }
|