winform自定义分页控件

时间:2024-10-02 17:38:08

1、控件代码:

public partial class PagerControl : UserControl {
#region 构造函数 public PagerControl() {
InitializeComponent();
} #endregion #region 分页字段和属性 private int pageIndex = ;
/// <summary>
/// 当前页面
/// </summary>
public virtual int PageIndex {
get { return pageIndex; }
set { pageIndex = value; }
} private int pageSize = ;
/// <summary>
/// 每页记录数
/// </summary>
public virtual int PageSize {
get { return pageSize; }
set { pageSize = value; }
} private int recordCount = ;
/// <summary>
/// 总记录数
/// </summary>
public virtual int RecordCount {
get { return recordCount; }
set { recordCount = value; }
} private int pageCount = ;
/// <summary>
/// 总页数
/// </summary>
public int PageCount {
get {
if (pageSize != ) {
pageCount = GetPageCount();
}
return pageCount;
}
}
/// <summary>
/// 计算总页数
/// </summary>
/// <returns></returns>
private int GetPageCount() {
if (PageSize == ) {
return ;
}
int pageCount = RecordCount / PageSize;
if (RecordCount % PageSize == ) {
pageCount = RecordCount / PageSize;
}
else {
pageCount = RecordCount / PageSize + ;
}
return pageCount;
}
#endregion public event EventHandler OnPageChanged; /// <summary>
/// 外部调用
/// </summary>
public void DrawControl(int count) {
recordCount = count;
DrawControl(false);
}
/// <summary>
/// 页面控件呈现
/// </summary>
private void DrawControl(bool callEvent) {
this.GoItem.Text = "跳转";
this.CurrentItem.Text = PageIndex.ToString();
this.TolCountItem.Text = RecordCount.ToString();
this.PageSizeItem.Text = PageSize.ToString(); if (callEvent && OnPageChanged != null) {
OnPageChanged(this, null);//当前分页数字改变时,触发委托事件
}
SetFormCtrEnabled();
if (PageCount == )//有且仅有一页
{
this.MoveFirstItem.Enabled = false;
this.MovePreviousItem.Enabled = false;
this.MoveNextItem.Enabled = false;
this.MoveLastItem.Enabled = false;
}
else if (PageIndex == )//第一页
{
this.MoveFirstItem.Enabled = false;
this.MovePreviousItem.Enabled = false;
}
else if (PageIndex == PageCount)//最后一页
{
this.MoveNextItem.Enabled = false;
this.MoveLastItem.Enabled = false;
}
} /// <summary>
/// 设置控件可用性
/// </summary>
private void SetFormCtrEnabled() {
this.CurrentItem.Enabled = true;
this.MoveFirstItem.Enabled = true;
this.MovePreviousItem.Enabled = true;
this.MoveNextItem.Enabled = true;
this.MoveLastItem.Enabled = true;
this.GoItem.Enabled = true;
} /// <summary>
/// 首页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MoveFirstItem_Click(object sender, EventArgs e) {
PageIndex = ;
DrawControl(true);
} /// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MovePreviousItem_Click(object sender, EventArgs e) {
PageIndex = Math.Max(, PageIndex - );
DrawControl(true);
} /// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MoveNextItem_Click(object sender, EventArgs e) {
PageIndex = Math.Min(PageCount, PageIndex + );
DrawControl(true);
} /// <summary>
/// 末页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MoveLastItem_Click(object sender, EventArgs e) {
PageIndex = PageCount;
DrawControl(true);
} /// <summary>
/// 跳转
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void GoItem_Click(object sender, EventArgs e) {
int num = ;
if (int.TryParse(this.CurrentItem.Text.Trim(), out num) && num > ) {
PageIndex = num;
DrawControl(true);
}
} /// <summary>
/// 跳转页数限制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CurrentItem_TextChanged(object sender, EventArgs e) {
int num = ;
if (int.TryParse(this.CurrentItem.Text.Trim(), out num) && num > ) {
if (num > PageCount) {
this.CurrentItem.Text = PageCount.ToString();
}
}
} /// <summary>
/// 页数Enter键
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CurrentItem_KeyPress(object sender, KeyPressEventArgs e) {
GoItem_Click(null, null);
} private bool isTextChanged = false; /// <summary>
/// 页数改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PageSizeItem_TextChanged(object sender, EventArgs e) {
int num = ;
if (!int.TryParse(this.PageSizeItem.Text.Trim(), out num) || num <= ) {
num = ;
this.PageSizeItem.Text = "";
}
else {
isTextChanged = true;
}
pageSize = num;
} /// <summary>
/// 页数Enter事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PageSizeItem_KeyPress(object sender, KeyPressEventArgs e) {
if (e.KeyChar == System.Convert.ToChar()) {
if (isTextChanged) {
isTextChanged = false;
MoveFirstItem_Click(null, null);
}
}
}
}

2、调用代码:

        /// <summary>
/// 加载数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SelectForm_Load(object sender, EventArgs e) {
LoadData();
pagerControl1.OnPageChanged += new EventHandler(pagerControl1_OnPageChanged);
} /// <summary>
/// 分页控件改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void pagerControl1_OnPageChanged(object sender, EventArgs e) {
LoadData();
} /// <summary>
/// 加载数据
/// </summary>
/// <param name="index"></param>
/// <param name="pagesize"></param>
private void LoadData() {
List<Template> templateList = new List<Template>();
HttpHelper http = new HttpHelper();
string url = string.Format("https://www.baidu.com?page={0}&pagecount={1}", this.pagerControl1.PageIndex, this.pagerControl1.PageSize);
string result = http.CollectHtml(url, "", HttpHelper.Method.GET, HttpHelper.HtmlEncoding.UTF8, HttpHelper.HtmlEncoding.UTF8);
var resultData = JsonConvert.DeserializeObject<JObject>(result);
//分页信息
if (resultData["flipinfo"] != null) {
Page page = JsonConvert.DeserializeObject<Page>(resultData["flipinfo"].ToString());
if (page != null) {
pagerControl1.DrawControl(page.total);
}
}
}

  以上是实际项目中使用的代码,供大家参考。