新分页控件
太久没写Blog了,最近一段时间更是连博客园都没上过,繁忙的工作让我连停下来冲冲电的时间都没有(难得的空闲时间都用来魔兽世界了),最近总算有点空闲时间,看看以前写的分页控件,是在是太丑陋了,修改了一下。
设计上的考虑,界面设计上显示总页数、当前页数、可以前翻、后翻、跳转到指定页面。页面改变时触发currentPageChanged事件,使用此控件只要调用Init(int count,int perpage)就可以了。
注:count 是显示的总数,perpage 每页显示的个数,调用init 方法会触发currentPageChanged事件。在currentPageChanged事件处理方法中获取当前页CurrentPage
代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace LMAS
{
/// <summary>
/// Summary description for UserControl1.
/// </summary>
public class PageControl : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Label lblPageNum;
private System.Windows.Forms.LinkLabel lnkFirst;
private System.Windows.Forms.LinkLabel lnkPrev;
private System.Windows.Forms.LinkLabel lnkNext;
private System.Windows.Forms.LinkLabel lnkLast;
private System.Windows.Forms.TextBox txtBxNumber;
private System.Windows.Forms.Label label2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private int displayCount;
private int perPage;
private int pageCount;
private System.Windows.Forms.Button btnGo;
private int currentPage;
private int prevPage;
public event EventHandler currentPageChanged;
public int DisplayCount
{
get { return displayCount; }
}
public int PerPage
{
get { return perPage; }
}
public int PageCount
{
get { return pageCount; }
}
public int CurrentPage
{
get { return currentPage; }
}
public PageControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
displayCount = 0;
perPage = 0;
pageCount = 1;
currentPage = 1;
prevPage = 0;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( 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()
{
this.lblPageNum = new System.Windows.Forms.Label();
this.lnkFirst = new System.Windows.Forms.LinkLabel();
this.lnkPrev = new System.Windows.Forms.LinkLabel();
this.lnkNext = new System.Windows.Forms.LinkLabel();
this.lnkLast = new System.Windows.Forms.LinkLabel();
this.txtBxNumber = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.btnGo = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblPageNum
//
this.lblPageNum.Location = new System.Drawing.Point(0, 0);
this.lblPageNum.Name = "lblPageNum";
this.lblPageNum.Size = new System.Drawing.Size(88, 23);
this.lblPageNum.TabIndex = 0;
this.lblPageNum.Text = "1/1";
this.lblPageNum.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// lnkFirst
//
this.lnkFirst.LinkBehavior = System.Windows.Forms.LinkBehavior.NeverUnderline;
this.lnkFirst.Location = new System.Drawing.Point(88, 0);
this.lnkFirst.Name = "lnkFirst";
this.lnkFirst.Size = new System.Drawing.Size(24, 23);
this.lnkFirst.TabIndex = 1;
this.lnkFirst.TabStop = true;
this.lnkFirst.Text = "|<";
this.lnkFirst.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.lnkFirst.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.PageLinkClicked);
//
// lnkPrev
//
this.lnkPrev.LinkBehavior = System.Windows.Forms.LinkBehavior.NeverUnderline;
this.lnkPrev.Location = new System.Drawing.Point(112, 1);
this.lnkPrev.Name = "lnkPrev";
this.lnkPrev.Size = new System.Drawing.Size(24, 23);
this.lnkPrev.TabIndex = 2;
this.lnkPrev.TabStop = true;
this.lnkPrev.Text = "<";
this.lnkPrev.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.lnkPrev.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.PageLinkClicked);
//
// lnkNext
//
this.lnkNext.LinkBehavior = System.Windows.Forms.LinkBehavior.NeverUnderline;
this.lnkNext.Location = new System.Drawing.Point(144, 1);
this.lnkNext.Name = "lnkNext";
this.lnkNext.Size = new System.Drawing.Size(24, 23);
this.lnkNext.TabIndex = 3;
this.lnkNext.TabStop = true;
this.lnkNext.Text = ">";
this.lnkNext.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.lnkNext.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.PageLinkClicked);
//
// lnkLast
//
this.lnkLast.LinkBehavior = System.Windows.Forms.LinkBehavior.NeverUnderline;
this.lnkLast.Location = new System.Drawing.Point(168, 0);
this.lnkLast.Name = "lnkLast";
this.lnkLast.Size = new System.Drawing.Size(24, 23);
this.lnkLast.TabIndex = 4;
this.lnkLast.TabStop = true;
this.lnkLast.Text = ">|";
this.lnkLast.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.lnkLast.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.PageLinkClicked);
//
// txtBxNumber
//
this.txtBxNumber.Location = new System.Drawing.Point(256, 0);
this.txtBxNumber.Name = "txtBxNumber";
this.txtBxNumber.Size = new System.Drawing.Size(56, 21);
this.txtBxNumber.TabIndex = 5;
this.txtBxNumber.Text = "";
this.txtBxNumber.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.txtBxNumber.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtBxNumber_KeyPress);
this.txtBxNumber.TextChanged += new System.EventHandler(this.txtBxNumber_TextChanged);
//
// label2
//
this.label2.Location = new System.Drawing.Point(312, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(24, 23);
this.label2.TabIndex = 7;
this.label2.Text = "页";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btnGo
//
this.btnGo.Location = new System.Drawing.Point(344, 0);
this.btnGo.Name = "btnGo";
this.btnGo.Size = new System.Drawing.Size(40, 21);
this.btnGo.TabIndex = 8;
this.btnGo.Text = "跳转";
this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
//
// PageControl
//
this.Controls.Add(this.btnGo);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtBxNumber);
this.Controls.Add(this.lnkLast);
this.Controls.Add(this.lnkNext);
this.Controls.Add(this.lnkPrev);
this.Controls.Add(this.lnkFirst);
this.Controls.Add(this.lblPageNum);
this.Name = "PageControl";
this.Size = new System.Drawing.Size(400, 22);
this.ResumeLayout(false);
}
#endregion
public void Init(int count,int perpage)
{
displayCount = Math.Max(count,1);
perPage = Math.Min(perpage,displayCount);
pageCount = displayCount / perPage;
if (displayCount % perPage != 0)
pageCount ++;
currentPage = 1;
DrawControl();
}
private void DrawControl()
{
lblPageNum.Text = currentPage.ToString() + "/" + pageCount.ToString();
if (prevPage != currentPage && currentPageChanged != null)
currentPageChanged(this,null);
prevPage = currentPage;
}
private void txtBxNumber_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar) && e.KeyChar != (Char)8)
e.Handled = true;
}
//页数选择限制
private void txtBxNumber_TextChanged(object sender, System.EventArgs e)
{
if (txtBxNumber.Text.Length > 0 && int.Parse(txtBxNumber.Text) > pageCount)
{
txtBxNumber.Text = pageCount.ToString();
}
}
private void PageLinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
LinkLabel lnkLbl = sender as LinkLabel;
if (lnkLbl == null)
return;
switch (lnkLbl.Name)
{
case "lnkFirst":
currentPage = 1;
break;
case "lnkLast":
currentPage = pageCount;
break;
case "lnkPrev":
currentPage = Math.Max(1,currentPage - 1);
break;
case "lnkNext":
currentPage = Math.Min(pageCount,currentPage + 1);
break;
}
DrawControl();
}
private void btnGo_Click(object sender, System.EventArgs e)
{
//忽略错误
if (txtBxNumber.Text.Length == 0 || int.Parse(txtBxNumber.Text) > pageCount)
return;
currentPage = int.Parse(txtBxNumber.Text);
DrawControl();
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace LMAS
{
/// <summary>
/// Summary description for UserControl1.
/// </summary>
public class PageControl : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Label lblPageNum;
private System.Windows.Forms.LinkLabel lnkFirst;
private System.Windows.Forms.LinkLabel lnkPrev;
private System.Windows.Forms.LinkLabel lnkNext;
private System.Windows.Forms.LinkLabel lnkLast;
private System.Windows.Forms.TextBox txtBxNumber;
private System.Windows.Forms.Label label2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private int displayCount;
private int perPage;
private int pageCount;
private System.Windows.Forms.Button btnGo;
private int currentPage;
private int prevPage;
public event EventHandler currentPageChanged;
public int DisplayCount
{
get { return displayCount; }
}
public int PerPage
{
get { return perPage; }
}
public int PageCount
{
get { return pageCount; }
}
public int CurrentPage
{
get { return currentPage; }
}
public PageControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
displayCount = 0;
perPage = 0;
pageCount = 1;
currentPage = 1;
prevPage = 0;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( 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()
{
this.lblPageNum = new System.Windows.Forms.Label();
this.lnkFirst = new System.Windows.Forms.LinkLabel();
this.lnkPrev = new System.Windows.Forms.LinkLabel();
this.lnkNext = new System.Windows.Forms.LinkLabel();
this.lnkLast = new System.Windows.Forms.LinkLabel();
this.txtBxNumber = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.btnGo = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblPageNum
//
this.lblPageNum.Location = new System.Drawing.Point(0, 0);
this.lblPageNum.Name = "lblPageNum";
this.lblPageNum.Size = new System.Drawing.Size(88, 23);
this.lblPageNum.TabIndex = 0;
this.lblPageNum.Text = "1/1";
this.lblPageNum.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// lnkFirst
//
this.lnkFirst.LinkBehavior = System.Windows.Forms.LinkBehavior.NeverUnderline;
this.lnkFirst.Location = new System.Drawing.Point(88, 0);
this.lnkFirst.Name = "lnkFirst";
this.lnkFirst.Size = new System.Drawing.Size(24, 23);
this.lnkFirst.TabIndex = 1;
this.lnkFirst.TabStop = true;
this.lnkFirst.Text = "|<";
this.lnkFirst.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.lnkFirst.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.PageLinkClicked);
//
// lnkPrev
//
this.lnkPrev.LinkBehavior = System.Windows.Forms.LinkBehavior.NeverUnderline;
this.lnkPrev.Location = new System.Drawing.Point(112, 1);
this.lnkPrev.Name = "lnkPrev";
this.lnkPrev.Size = new System.Drawing.Size(24, 23);
this.lnkPrev.TabIndex = 2;
this.lnkPrev.TabStop = true;
this.lnkPrev.Text = "<";
this.lnkPrev.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.lnkPrev.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.PageLinkClicked);
//
// lnkNext
//
this.lnkNext.LinkBehavior = System.Windows.Forms.LinkBehavior.NeverUnderline;
this.lnkNext.Location = new System.Drawing.Point(144, 1);
this.lnkNext.Name = "lnkNext";
this.lnkNext.Size = new System.Drawing.Size(24, 23);
this.lnkNext.TabIndex = 3;
this.lnkNext.TabStop = true;
this.lnkNext.Text = ">";
this.lnkNext.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.lnkNext.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.PageLinkClicked);
//
// lnkLast
//
this.lnkLast.LinkBehavior = System.Windows.Forms.LinkBehavior.NeverUnderline;
this.lnkLast.Location = new System.Drawing.Point(168, 0);
this.lnkLast.Name = "lnkLast";
this.lnkLast.Size = new System.Drawing.Size(24, 23);
this.lnkLast.TabIndex = 4;
this.lnkLast.TabStop = true;
this.lnkLast.Text = ">|";
this.lnkLast.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.lnkLast.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.PageLinkClicked);
//
// txtBxNumber
//
this.txtBxNumber.Location = new System.Drawing.Point(256, 0);
this.txtBxNumber.Name = "txtBxNumber";
this.txtBxNumber.Size = new System.Drawing.Size(56, 21);
this.txtBxNumber.TabIndex = 5;
this.txtBxNumber.Text = "";
this.txtBxNumber.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.txtBxNumber.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtBxNumber_KeyPress);
this.txtBxNumber.TextChanged += new System.EventHandler(this.txtBxNumber_TextChanged);
//
// label2
//
this.label2.Location = new System.Drawing.Point(312, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(24, 23);
this.label2.TabIndex = 7;
this.label2.Text = "页";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btnGo
//
this.btnGo.Location = new System.Drawing.Point(344, 0);
this.btnGo.Name = "btnGo";
this.btnGo.Size = new System.Drawing.Size(40, 21);
this.btnGo.TabIndex = 8;
this.btnGo.Text = "跳转";
this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
//
// PageControl
//
this.Controls.Add(this.btnGo);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtBxNumber);
this.Controls.Add(this.lnkLast);
this.Controls.Add(this.lnkNext);
this.Controls.Add(this.lnkPrev);
this.Controls.Add(this.lnkFirst);
this.Controls.Add(this.lblPageNum);
this.Name = "PageControl";
this.Size = new System.Drawing.Size(400, 22);
this.ResumeLayout(false);
}
#endregion
public void Init(int count,int perpage)
{
displayCount = Math.Max(count,1);
perPage = Math.Min(perpage,displayCount);
pageCount = displayCount / perPage;
if (displayCount % perPage != 0)
pageCount ++;
currentPage = 1;
DrawControl();
}
private void DrawControl()
{
lblPageNum.Text = currentPage.ToString() + "/" + pageCount.ToString();
if (prevPage != currentPage && currentPageChanged != null)
currentPageChanged(this,null);
prevPage = currentPage;
}
private void txtBxNumber_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar) && e.KeyChar != (Char)8)
e.Handled = true;
}
//页数选择限制
private void txtBxNumber_TextChanged(object sender, System.EventArgs e)
{
if (txtBxNumber.Text.Length > 0 && int.Parse(txtBxNumber.Text) > pageCount)
{
txtBxNumber.Text = pageCount.ToString();
}
}
private void PageLinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
LinkLabel lnkLbl = sender as LinkLabel;
if (lnkLbl == null)
return;
switch (lnkLbl.Name)
{
case "lnkFirst":
currentPage = 1;
break;
case "lnkLast":
currentPage = pageCount;
break;
case "lnkPrev":
currentPage = Math.Max(1,currentPage - 1);
break;
case "lnkNext":
currentPage = Math.Min(pageCount,currentPage + 1);
break;
}
DrawControl();
}
private void btnGo_Click(object sender, System.EventArgs e)
{
//忽略错误
if (txtBxNumber.Text.Length == 0 || int.Parse(txtBxNumber.Text) > pageCount)
return;
currentPage = int.Parse(txtBxNumber.Text);
DrawControl();
}
}
}
设计图如下: