运行效果:

控件结构图:

实现代码:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace KK.Pagers
{
public partial class Pagers : UserControl
{
#region 定义、构造
private int _pageIndex = 1;
/// <summary>
/// 当前页数
/// </summary>
public int PageIndex
{
get { return _pageIndex; }
set { _pageIndex = value; }
}
private int _pageSize;
/// <summary>
/// 每一页显示的数据量
/// </summary>
public int PageSize
{
get { return _pageSize; }
set { _pageSize = value; }
}
private int _pageCount;
/// <summary>
/// 总数据数
/// </summary>
public int PageCount
{
get
{
if (PageSize != 0)
{
_pageCount = GetPageCount();
}
return _pageCount;
}
}
private int _totalCount = 20;
/// <summary>
/// 总数
/// </summary>
public int TotalCount
{
get { return _totalCount; }
set
{
_totalCount = value;
}
}
/// <summary>
/// 首页背景图
/// </summary>
public Image FirstBackgroundImage
{
get { return pboxFirst.Image; }
set
{
pboxFirst.Image = value;
}
}
/// <summary>
/// 上一页背景图
/// </summary>
public Image LastBackgroundImage
{
get { return this.pboxUp.Image; }
set
{
pboxUp.Image = value;
}
}
/// <summary>
/// 下一个背景图
/// </summary>
public Image NextBackgroundImage
{
get { return this.pboxNext.Image; }
set
{
pboxNext.Image = value;
}
}
/// <summary>
/// 尾页背景图
/// </summary>
public Image EndBackgroundImage
{
get { return this.pboxEnd.Image; }
set
{
pboxEnd.Image = value;
}
}
/// <summary>
/// 构造
/// </summary>
public Pagers()
{
InitializeComponent();
this.Load += Pagers_Load;
}
#endregion
#region event
void Pagers_Load(object sender, EventArgs e)
{
if (PageIndex <= 0)
PageIndex = 1;
}
[Browsable(true), Description("分页改变事件"), Category("操作")]
public event EventHandler OnPageChanged;
//首页
private void pboxPageFirst_Click(object sender, EventArgs e)
{
PageIndex = 1;
DrawControl(true);
}
//上一页
private void pboxPre_Click(object sender, EventArgs e)
{
if (PageIndex > 1)
{
PageIndex--;
}
DrawControl(true);
}
//下一页
private void pboxNext_Click(object sender, EventArgs e)
{
if (PageIndex < PageCount)
{
PageIndex++;
}
DrawControl(true);
}
//尾页
private void pboxLast_Click(object sender, EventArgs e)
{
PageIndex = PageCount;
DrawControl(true);
}
private void ctboxCurrentPageIndex_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
int page = -1;
if (int.TryParse(ctboxCurrentPageIndex.Text, out page)
&& int.Parse(ctboxCurrentPageIndex.Text) > 0
&& int.Parse(ctboxCurrentPageIndex.Text) <= PageCount
&& PageIndex != int.Parse(ctboxCurrentPageIndex.Text))
{
PageIndex = int.Parse(ctboxCurrentPageIndex.Text);
DrawControl(true);
}
else
{
ctboxCurrentPageIndex.Text = PageIndex.ToString();
ctboxCurrentPageIndex.SelectionStart = ctboxCurrentPageIndex.TextLength;
}
}
}
private void ctboxCurrentPageIndex_TextChanged(object sender, EventArgs e)
{
Regex reg = new Regex("^[0-9]*$");
if (!reg.IsMatch(ctboxCurrentPageIndex.Text))
{
ctboxCurrentPageIndex.Text = PageIndex.ToString();
ctboxCurrentPageIndex.SelectionStart = ctboxCurrentPageIndex.TextLength;
}
if (PageCount.Equals(0))
{
DisplayImage(this.pboxFirst, global::KK.Pagers.Properties.Resources.Page_First1);
DisplayImage(this.pboxUp, global::KK.Pagers.Properties.Resources.Page_Last1);
DisplayImage(this.pboxNext, global::KK.Pagers.Properties.Resources.Page_Next1);
DisplayImage(this.pboxEnd, global::KK.Pagers.Properties.Resources.Page_End1);
return;
}
//首页且不止一页数据
if (PageIndex.Equals(1) && !PageIndex.Equals(PageCount))
{
DisplayImage(this.pboxFirst, global::KK.Pagers.Properties.Resources.Page_First1);
DisplayImage(this.pboxUp, global::KK.Pagers.Properties.Resources.Page_Last1);
DisplayImage(this.pboxNext, global::KK.Pagers.Properties.Resources.Page_Next);
DisplayImage(this.pboxEnd, global::KK.Pagers.Properties.Resources.Page_End);
return;
}
//只有一页数据
if (PageIndex.Equals(1) && PageIndex.Equals(PageCount))
{
DisplayImage(this.pboxFirst, global::KK.Pagers.Properties.Resources.Page_First1);
DisplayImage(this.pboxUp, global::KK.Pagers.Properties.Resources.Page_Last1);
DisplayImage(this.pboxNext, global::KK.Pagers.Properties.Resources.Page_Next1);
DisplayImage(this.pboxEnd, global::KK.Pagers.Properties.Resources.Page_End1);
return;
}
//尾页
if (PageIndex == PageCount)
{
DisplayImage(this.pboxFirst, global::KK.Pagers.Properties.Resources.Page_First);
DisplayImage(this.pboxUp, global::KK.Pagers.Properties.Resources.Page_Last);
DisplayImage(this.pboxNext, global::KK.Pagers.Properties.Resources.Page_Next1);
DisplayImage(this.pboxEnd, global::KK.Pagers.Properties.Resources.Page_End1);
return;
}
//大于第一页
if (PageIndex > 1 && PageIndex < PageCount)
{
DisplayImage(this.pboxFirst, global::KK.Pagers.Properties.Resources.Page_First);
DisplayImage(this.pboxUp, global::KK.Pagers.Properties.Resources.Page_Last);
DisplayImage(this.pboxNext, global::KK.Pagers.Properties.Resources.Page_Next);
DisplayImage(this.pboxEnd, global::KK.Pagers.Properties.Resources.Page_End);
return;
}
}
#endregion
#region private method
/// <summary>
/// 设置控件是否显示
/// </summary>
/// <param name="pb">PictureBox pb</param>
/// <param name="img">图片</param>
private void DisplayImage(PictureBox pb, Image img)
{
if (img != null) pb.Image = img;
}
/// <summary>
/// 获取页数
/// </summary>
/// <returns>页数</returns>
private int GetPageCount()
{
if (PageSize == 0)
{
return 0;
}
int pageCount = TotalCount / PageSize;
if (TotalCount % PageSize == 0)
{
pageCount = TotalCount / PageSize;
}
else
{
pageCount = TotalCount / PageSize + 1;
}
return pageCount;
}
/// <summary>
/// 显示页数
/// </summary>
/// <param name="count">页数</param>
public void DrawControl(int count)
{
TotalCount = count;
this.lblPageCountShow.Text = string.Format("共{0}页", PageCount);
DrawControl(false);
}
/// <summary>
/// 分页控件可用性设置
/// </summary>
/// <param name="callEvent">callEvent</param>
private void DrawControl(bool callEvent)
{
this.ctboxCurrentPageIndex.Text = PageIndex.ToString();
this.ctboxCurrentPageIndex.SelectionStart = ctboxCurrentPageIndex.TextLength;
if (callEvent && OnPageChanged != null)
OnPageChanged(this, null);
SetFormCtrEnabled();
if (PageCount == 0)
{
this.ctboxCurrentPageIndex.Text = "1";
this.ctboxCurrentPageIndex.SelectionStart = ctboxCurrentPageIndex.TextLength;
this.pboxFirst.Enabled = false;
this.pboxUp.Enabled = false;
this.pboxNext.Enabled = false;
this.pboxEnd.Enabled = false;
}
if (PageCount == 1)
{
this.ctboxCurrentPageIndex.Text = "1";
this.ctboxCurrentPageIndex.SelectionStart = ctboxCurrentPageIndex.TextLength;
this.pboxFirst.Enabled = false;
this.pboxUp.Enabled = false;
this.pboxNext.Enabled = false;
this.pboxEnd.Enabled = false;
}
else if (PageIndex == 1)
{
this.ctboxCurrentPageIndex.Text = "1";
this.ctboxCurrentPageIndex.SelectionStart = ctboxCurrentPageIndex.TextLength;
this.pboxFirst.Enabled = false;
this.pboxUp.Enabled = false;
}
else if (PageIndex == PageCount)
{
this.ctboxCurrentPageIndex.Text = PageCount.ToString();
this.ctboxCurrentPageIndex.SelectionStart = ctboxCurrentPageIndex.TextLength;
this.pboxNext.Enabled = false;
this.pboxEnd.Enabled = false;
}
}
private void SetFormCtrEnabled()
{
this.pboxFirst.Enabled = true;
this.pboxNext.Enabled = true;
this.pboxUp.Enabled = true;
this.pboxEnd.Enabled = true;
}
#endregion
private void Pbox_EnabledChanged(object sender, EventArgs e)
{
PictureBox pbox = sender as PictureBox;
bool isEnable = pbox.Enabled;
Image pageimage;
switch (pbox.Name)
{
case "pboxFirst":
pageimage = isEnable ? global::KK.Pagers.Properties.Resources.Page_First : global::KK.Pagers.Properties.Resources.Page_First1;
break;
case "pboxNext":
pageimage = isEnable ? global::KK.Pagers.Properties.Resources.Page_Next : global::KK.Pagers.Properties.Resources.Page_Next1;
break;
case "pboxUp":
pageimage = isEnable ? global::KK.Pagers.Properties.Resources.Page_Last : global::KK.Pagers.Properties.Resources.Page_Last1;
break;
default:
pageimage = isEnable ? global::KK.Pagers.Properties.Resources.Page_End : global::KK.Pagers.Properties.Resources.Page_End1;
break;
}
DisplayImage(pbox, pageimage);
}
private void Pagers_Load_1(object sender, EventArgs e)
{
if (GetPageCount() > 0) ctboxCurrentPageIndex.Text = "1";
else ctboxCurrentPageIndex.Text = "0";
}
}
}
调用方式:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Demo
{
public partial class Form2 : Form
{
List<StudentInfo> list = new List<StudentInfo>();
public Form2()
{
InitializeComponent();
this.dgvList.AutoGenerateColumns = false;
this.pagingList.PageIndex = 1;
for (int i = 1; i <= 33; i++)
{
StudentInfo stu = new StudentInfo();
stu.Name = "学生" + i;
stu.Age = i;
list.Add(stu);
}
}
private void Form2_Load(object sender, EventArgs e)
{
GetPageData();
}
private void GetPageData()
{
List<StudentInfo> dataSource = list
.Skip((this.pagingList.PageIndex - 1) * this.pagingList.PageSize)
.Take(this.pagingList.PageSize).ToList();
var count = Math.Ceiling(double.Parse(Convert.ToString(list.Count / dataSource.Count)));
this.dgvList.DataSource = dataSource;
this.pagingList.TotalCount = list.Count;
//调用page的公用方法
this.pagingList.DrawControl(list.Count);
}
private void pagingList_OnPageChanged(object sender, EventArgs e)
{
GetPageData();
}
}
public class StudentInfo
{
public string Name { get; set; }
public int Age { get; set; }
}
}