分页控件如下图所示:
实现方式如下:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace PISS.View.CustomControl
{
public partial class Pagers : UserControl
{
#region 定义、构造
/// <summary>
/// Page_Last.png 上一页
/// </summary>
private string PageUp = Application.StartupPath + "\\Images\\Page_Last.png";
/// <summary>
/// Page_Last1.png 上一页 不可用
/// </summary>
private string PageUp1 = Application.StartupPath + "\\Images\\Page_Last1.png";
/// <summary>
/// Page_Next.png 下一页
/// </summary>
private string PageNext = Application.StartupPath + "\\Images\\Page_Next.png";
/// <summary>
/// Page_Next1.png 下一页 不可用
/// </summary>
private string PageNext1 = Application.StartupPath + "\\Images\\Page_Next1.png";
/// <summary>
/// Page_First.png 首页
/// </summary>
private string PageFirst = Application.StartupPath + "\\Images\\Page_First.png";
/// <summary>
/// Page_First1.png 首页 不可用
/// </summary>
private string PageFirst1 = Application.StartupPath + "\\Images\\Page_First1.png";
/// <summary>
/// Page_End.png 尾页
/// </summary>
private string PageEnd = Application.StartupPath + "\\Images\\Page_End.png";
/// <summary>
/// Page_End1.png 尾页 不可用
/// </summary>
private string PageEnd1 = Application.StartupPath + "\\Images\\Page_End1.png";
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, PageFirst1);
DisplayImage(this.pboxUp, PageUp1);
DisplayImage(this.pboxNext, PageNext1);
DisplayImage(this.pboxEnd, PageEnd1);
return;
}
//首页且不止一页数据
if (PageIndex.Equals(1) && !PageIndex.Equals(PageCount))
{
DisplayImage(this.pboxFirst, PageFirst1);
DisplayImage(this.pboxUp, PageUp1);
DisplayImage(this.pboxNext, PageNext);
DisplayImage(this.pboxEnd, PageEnd);
return;
}
//只有一页数据
if (PageIndex.Equals(1) && PageIndex.Equals(PageCount))
{
DisplayImage(this.pboxFirst, PageFirst1);
DisplayImage(this.pboxUp, PageUp1);
DisplayImage(this.pboxNext, PageNext1);
DisplayImage(this.pboxEnd, PageEnd1);
return;
}
//尾页
if (PageIndex == PageCount)
{
DisplayImage(this.pboxFirst, PageFirst);
DisplayImage(this.pboxUp, PageUp);
DisplayImage(this.pboxNext, PageNext1);
DisplayImage(this.pboxEnd, PageEnd1);
return;
}
//大于第一页
if (PageIndex > 1 && PageIndex < PageCount)
{
DisplayImage(this.pboxFirst, PageFirst);
DisplayImage(this.pboxUp, PageUp);
DisplayImage(this.pboxNext, PageNext);
DisplayImage(this.pboxEnd, PageEnd);
return;
}
}
#endregion
#region private method
/// <summary>
/// 设置控件是否显示
/// </summary>
/// <param name="pb">PictureBox pb</param>
/// <param name="filePath">图片所在路径</param>
private void DisplayImage(PictureBox pb, string filePath)
{
Image img = Image.FromFile(filePath);
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;
string pageimage = string.Empty;
switch (pbox.Name)
{
case "pboxFirst":
pageimage = isEnable ? PageFirst : PageFirst1;
break;
case "pboxNext":
pageimage = isEnable ? PageNext : PageNext1;
break;
case "pboxUp":
pageimage = isEnable ? PageUp : PageUp1;
break;
default:
pageimage = isEnable ? PageEnd : PageEnd1;
break;
}
DisplayImage(pbox, pageimage);
}
}
}