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 mywinformapp
{
public partial class PagerControl : UserControl
{
private int pageIndex = 1;
private int pageSize = 10;
private int recordCount = 0;
private int pageCount = 0;
#region 构造函数
public PagerControl()
{
InitializeComponent();
}
#endregion
#region 分页字段和属性
/// <summary>
/// 当前页数
/// </summary>
public virtual int PageIndex
{
get { return pageIndex; }
set { pageIndex = value; }
}
/// <summary>
/// 每页记录数
/// </summary>
public virtual int PageSize
{
get { return pageSize; }
set { pageSize = value; }
}
/// <summary>
/// 总记录数
/// </summary>
public virtual int RecordCount
{
get { return recordCount; }
set { recordCount = value; }
}
/// <summary>
/// 总页数
/// </summary>
public int PageCount
{
get
{
if (pageSize != 0)
{
pageCount = GetPageCount();
}
return pageCount;
}
}
#endregion
#region 页码变化时触发事件
public event EventHandler OnPageChanged;
#endregion
#region 分页及相关事件功能实现
/// <summary>
/// 设窗体控件全部可用
/// </summary>
private void SetFormCtrEnabled()
{
linkFirst.Enabled = true;
linkPrevious.Enabled = true;
linkNext.Enabled = true;
linkLast.Enabled = true;
btnGo.Enabled = true;
}
/// <summary>
/// 计算总页数
/// </summary>
/// <returns></returns>
private int GetPageCount()
{
if (PageSize == 0)
{
return 0;
}
int pageCount = RecordCount / PageSize;
if (RecordCount % PageSize == 0)
{
pageCount = RecordCount / PageSize;
}
else
{
pageCount = RecordCount / PageSize + 1;
}
return pageCount;
}
/// <summary>
/// 用于客户端调用
/// </summary>
/// <param name="count">记录总条数</param>
public void DrawControl(int count)
{
recordCount = count;
DrawControl(false);
}
/// <summary>
/// 根据不同的条件,改变页面控件的呈现状态
/// </summary>
private void DrawControl(bool callEvent)
{
lblCurrentPage.Text = PageIndex.ToString();
lblPageCount.Text = PageCount.ToString();
lblTotalCount.Text = RecordCount.ToString();
txtPageSize.Text = PageSize.ToString();
if (callEvent && OnPageChanged != null)
{
OnPageChanged(this, null);//当前分页数字改变时,触发委托事件
}
SetFormCtrEnabled();
if (PageCount == 1)//有且仅有一页时
{
linkFirst.Enabled = false;
linkPrevious.Enabled = false;
linkNext.Enabled = false;
linkLast.Enabled = false;
btnGo.Enabled = false;
}
else if (PageIndex == 1)//当前页为第一页时
{
linkFirst.Enabled = false;
linkPrevious.Enabled = false;
}
else if (PageIndex == PageCount)//当前页为最后一页时
{
linkNext.Enabled = false;
linkLast.Enabled = false;
}
}
#endregion
#region 相关控件事件
//首页按钮
private void linkFirst_Click(object sender, EventArgs e)
{
PageIndex = 1;
DrawControl(true);
}
//上一页按钮
private void linkPrevious_Click(object sender, EventArgs e)
{
PageIndex = Math.Max(1, PageIndex - 1);
DrawControl(true);
}
//下一页按钮
private void linkNext_Click(object sender, EventArgs e)
{
PageIndex = Math.Min(PageCount, PageIndex + 1);
DrawControl(true);
}
//尾页按钮
private void linkLast_Click(object sender, EventArgs e)
{
PageIndex = PageCount;
DrawControl(true);
}
/// <summary>
/// 按下enter键,执行跳转页面功能
/// </summary>
private void txtPageNum_KeyPress(object sender, KeyPressEventArgs e)
{
btnGo_Click(null, null);
}
/// <summary>
/// 跳转页数限制
/// </summary>
private void txtPageNum_TextChanged(object sender, EventArgs e)
{
int num = 0;
if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
{ //TryParse 函数,将字符串转换成等效的整数,返回bool型,判断是否转换成功。
//输入除数字以外的字符是转换不成功的
if (num > PageCount) //输入数量大于最大页数时,文本框自动显示最大页数
{
txtPageNum.Text = PageCount.ToString();
}
}
}
/// <summary>
/// 跳转按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGo_Click(object sender, EventArgs e)
{
int num = 0;
if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
{
PageIndex = num;
DrawControl(true);
}
}
#endregion
bool isTextChanged = false;
/// <summary>
/// 每页显示的记录数改变时
/// </summary>
private void txtPageSize_TextChanged(object sender, EventArgs e)
{
int num = 0;
//输入不符合规范时,默认设置为100
if (!int.TryParse(txtPageSize.Text.Trim(), out num) || num <= 0)
{
num = 100;
txtPageSize.Text = "100";
}
else
{
isTextChanged = true;
}
pageSize = num;
}
/// <summary>
/// 光标离开 每页设置文本框时,显示到首页
private void txtPageSize_Leave(object sender, EventArgs e)
{
if (isTextChanged)
{
isTextChanged = false;
linkFirst_Click(null, null);
}
}
}
}
================================================================================================
namespace mywinformapp
{
partial class PagerControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PagerControl));
this.label1 = new System.Windows.Forms.Label();
this.txtPageSize = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.lblCurrentPage = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.lblPageCount = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.linkPrevious = new System.Windows.Forms.Button();
this.linkFirst = new System.Windows.Forms.Button();
this.linkNext = new System.Windows.Forms.Button();
this.linkLast = new System.Windows.Forms.Button();
this.txtPageNum = new System.Windows.Forms.TextBox();
this.btnGo = new System.Windows.Forms.Button();
this.lblTotalCount = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("PMingLiU", 10F);
this.label1.Location = new System.Drawing.Point(13, 11);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(42, 17);
this.label1.TabIndex = 0;
this.label1.Text = "每页";
//
// txtPageSize
//
this.txtPageSize.Location = new System.Drawing.Point(56, 5);
this.txtPageSize.Name = "txtPageSize";
this.txtPageSize.Size = new System.Drawing.Size(100, 25);
this.txtPageSize.TabIndex = 1;
this.txtPageSize.Text = "10";
this.txtPageSize.TextChanged += new System.EventHandler(this.txtPageSize_TextChanged);
this.txtPageSize.Leave += new System.EventHandler(this.txtPageSize_Leave);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("PMingLiU", 10F);
this.label2.Location = new System.Drawing.Point(162, 11);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(25, 17);
this.label2.TabIndex = 2;
this.label2.Text = "条";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("PMingLiU", 10F);
this.label3.Location = new System.Drawing.Point(209, 11);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(76, 17);
this.label3.TabIndex = 3;
this.label3.Text = "当前页:";
//
// lblCurrentPage
//
this.lblCurrentPage.AutoSize = true;
this.lblCurrentPage.Font = new System.Drawing.Font("PMingLiU", 10F);
this.lblCurrentPage.Location = new System.Drawing.Point(282, 7);
this.lblCurrentPage.Name = "lblCurrentPage";
this.lblCurrentPage.Size = new System.Drawing.Size(16, 17);
this.lblCurrentPage.TabIndex = 4;
this.lblCurrentPage.Text = "1";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Font = new System.Drawing.Font("PMingLiU", 10F);
this.label5.Location = new System.Drawing.Point(323, 10);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(12, 17);
this.label5.TabIndex = 5;
this.label5.Text = "/";
//
// lblPageCount
//
this.lblPageCount.AutoSize = true;
this.lblPageCount.Font = new System.Drawing.Font("PMingLiU", 10F);
this.lblPageCount.Location = new System.Drawing.Point(340, 8);
this.lblPageCount.Name = "lblPageCount";
this.lblPageCount.Size = new System.Drawing.Size(24, 17);
this.lblPageCount.TabIndex = 6;
this.lblPageCount.Text = "10";
//
// label7
//
this.label7.AutoSize = true;
this.label7.Font = new System.Drawing.Font("PMingLiU", 10F);
this.label7.Location = new System.Drawing.Point(403, 10);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(25, 17);
this.label7.TabIndex = 7;
this.label7.Text = "共";
//
// label8
//
this.label8.AutoSize = true;
this.label8.Font = new System.Drawing.Font("PMingLiU", 10F);
this.label8.Location = new System.Drawing.Point(494, 11);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(59, 17);
this.label8.TabIndex = 8;
this.label8.Text = "条记录";
//
// linkPrevious
//
this.linkPrevious.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("linkPrevious.BackgroundImage")));
this.linkPrevious.Location = new System.Drawing.Point(737, 3);
this.linkPrevious.Name = "linkPrevious";
this.linkPrevious.Size = new System.Drawing.Size(75, 30);
this.linkPrevious.TabIndex = 9;
this.linkPrevious.Text = "上页";
this.linkPrevious.UseVisualStyleBackColor = true;
this.linkPrevious.Click += new System.EventHandler(this.linkPrevious_Click);
//
// linkFirst
//
this.linkFirst.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("linkFirst.BackgroundImage")));
this.linkFirst.Location = new System.Drawing.Point(576, 3);
this.linkFirst.Name = "linkFirst";
this.linkFirst.Size = new System.Drawing.Size(75, 30);
this.linkFirst.TabIndex = 10;
this.linkFirst.Text = "首页";
this.linkFirst.UseVisualStyleBackColor = true;
this.linkFirst.Click += new System.EventHandler(this.linkFirst_Click);
//
// linkNext
//
this.linkNext.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("linkNext.BackgroundImage")));
this.linkNext.Location = new System.Drawing.Point(657, 3);
this.linkNext.Name = "linkNext";
this.linkNext.Size = new System.Drawing.Size(75, 30);
this.linkNext.TabIndex = 11;
this.linkNext.Text = "下页";
this.linkNext.UseVisualStyleBackColor = true;
this.linkNext.Click += new System.EventHandler(this.linkNext_Click);
//
// linkLast
//
this.linkLast.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("linkLast.BackgroundImage")));
this.linkLast.Location = new System.Drawing.Point(813, 3);
this.linkLast.Name = "linkLast";
this.linkLast.Size = new System.Drawing.Size(75, 30);
this.linkLast.TabIndex = 12;
this.linkLast.Text = "尾页";
this.linkLast.UseVisualStyleBackColor = true;
this.linkLast.Click += new System.EventHandler(this.linkLast_Click);
//
// txtPageNum
//
this.txtPageNum.Location = new System.Drawing.Point(894, 5);
this.txtPageNum.Name = "txtPageNum";
this.txtPageNum.Size = new System.Drawing.Size(75, 25);
this.txtPageNum.TabIndex = 13;
this.txtPageNum.Click += new System.EventHandler(this.txtPageNum_TextChanged);
this.txtPageNum.TextChanged += new System.EventHandler(this.txtPageNum_TextChanged);
//
// btnGo
//
this.btnGo.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnGo.BackgroundImage")));
this.btnGo.Location = new System.Drawing.Point(976, 3);
this.btnGo.Name = "btnGo";
this.btnGo.Size = new System.Drawing.Size(75, 30);
this.btnGo.TabIndex = 14;
this.btnGo.Text = "跳转";
this.btnGo.UseVisualStyleBackColor = true;
this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
//
// lblTotalCount
//
this.lblTotalCount.AutoSize = true;
this.lblTotalCount.Font = new System.Drawing.Font("PMingLiU", 10F);
this.lblTotalCount.Location = new System.Drawing.Point(422, 10);
this.lblTotalCount.Name = "lblTotalCount";
this.lblTotalCount.Size = new System.Drawing.Size(80, 17);
this.lblTotalCount.TabIndex = 15;
this.lblTotalCount.Text = "TotalCount";
//
// PagerControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.SkyBlue;
this.Controls.Add(this.lblTotalCount);
this.Controls.Add(this.btnGo);
this.Controls.Add(this.txtPageNum);
this.Controls.Add(this.linkLast);
this.Controls.Add(this.linkNext);
this.Controls.Add(this.linkFirst);
this.Controls.Add(this.linkPrevious);
this.Controls.Add(this.label8);
this.Controls.Add(this.label7);
this.Controls.Add(this.lblPageCount);
this.Controls.Add(this.label5);
this.Controls.Add(this.lblCurrentPage);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtPageSize);
this.Controls.Add(this.label1);
this.Name = "PagerControl";
this.Size = new System.Drawing.Size(1061, 37);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtPageSize;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label lblCurrentPage;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label lblPageCount;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Button linkPrevious;
private System.Windows.Forms.Button linkFirst;
private System.Windows.Forms.Button linkNext;
private System.Windows.Forms.Button linkLast;
private System.Windows.Forms.TextBox txtPageNum;
private System.Windows.Forms.Button btnGo;
private System.Windows.Forms.Label lblTotalCount;
}
}