程序很简单,用的时候需要提供pageCount(页数),pageShow(显示的页数),currPage(当前页号)
控件提供一个自定义的事件LnkLblClicked,当用户点击页面链接的时候就会触发这个事件。下面是程序的源码。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Collections;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.ComponentModel;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Drawing;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Data;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Windows.Forms;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace ActiveButton
<summary>
/// Summary description for PageControl.
/// </summary>
public class PageControl : System.Windows.Forms.UserControl
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
<summary>
/// Required designer variable.
/// </summary>
private int pageCount;
private int currPage;
// private int pageSize;
private int pageShow;
private int xDistance;
private bool bolPrev;
private bool bolNext;
private int start, end;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private EventHandler lnkLblClicked;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public event EventHandler LnkLblClicked
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add+= value;}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove-= value;}
}
protected virtual void OnLnkLblClicked(EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (lnkLblClicked != null)
lnkLblClicked(this,e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
public int PageCount
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set= value;}
}
public int CurrPage
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set= value;}
}
// public int PageSize
// {
// get{return pageSize;}
// set{pageSize = value;}
// }
public int PageShow
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set= value;}
}
public int XDistance
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set= value;}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private System.ComponentModel.Container components = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public PageControl()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// TODO: Add any initialization after the InitializeComponent call
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
public PageControl(int pageCount,int pageSize,int pageShow):this()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.pageCount = pageCount;
// this.pageSize = pageSize;
this.pageShow = pageShow;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
<summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if( disposing )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(components != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}
}
base.Dispose( disposing );
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region Component Designer generated code
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
<summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.SuspendLayout();
//
// PageControl
//
this.Name = "PageControl";
this.Size = new System.Drawing.Size(320, 24);
this.Load += new System.EventHandler(this.PageControl_Load);
this.ResumeLayout(false);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void PageControl_Load(object sender, System.EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
= 0;
bolPrev = pageCount > pageShow;
// currPage = pageCount;
end = pageCount;
start = Math.Max(1,end - pageShow + 1);
DrawControl();
}
private void DrawControl()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.Controls.Clear();
System.Windows.Forms.Label lblPageCount = new Label();
lblPageCount.Text = "共" + pageCount + "页";
this.AddControl(lblPageCount,new Point(8,8),new Size(40,16));
int x = lblPageCount.Bounds.Right;
int y = lblPageCount.Location.Y;
System.Windows.Forms.Control currCtl = lblPageCount;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//calculate page start and end
int pageShowCount = Math.Min(pageCount,pageShow);
// start = Math.Max(1,currPage - pageShow/2);
// end = start + pageShowCount -1;
// if (pageCount - currPage < pageShow/2)
// {
// end = pageCount;
// start = pageCount - pageShowCount + 1;
// }
bolPrev = start != 1;
bolNext = end != pageCount;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (bolPrev)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
System.Windows.Forms.LinkLabel lblPrev = new LinkLabel();
lblPrev.Text = "前一页";
x += xDistance;
((LinkLabel)lblPrev).LinkClicked += new LinkLabelLinkClickedEventHandler(lblCtl_Clicked);
this.AddControl(lblPrev,new Point(x,y),new Size(45, 16));
currCtl = lblPrev;
}
for (int i = start; i <= end; i ++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
x = currCtl.Bounds.Right;
System.Windows.Forms.Control lblCtl = null;
if ( i == currPage)
lblCtl = new System.Windows.Forms.Label();
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
= new System.Windows.Forms.LinkLabel();
((LinkLabel)lblCtl).LinkClicked += new LinkLabelLinkClickedEventHandler(lblCtl_Clicked);
}
lblCtl.Text = i.ToString();
x += xDistance;
this.AddControl(lblCtl,new Point(x,y),new Size(10, 16));
currCtl = lblCtl;
}
if (bolNext)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
System.Windows.Forms.LinkLabel lblNext = new LinkLabel();
lblNext.Text = "后一页";
x = currCtl.Bounds.Right;
x += xDistance;
((LinkLabel)lblNext).LinkClicked += new LinkLabelLinkClickedEventHandler(lblCtl_Clicked);
this.AddControl(lblNext,new Point(x,y),new Size(45, 16));
}
}
private void AddControl(Control ctlObj,Point point,Size size)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
= ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
ctlObj.Location = point;
int x = ctlObj.Text.Length * 10;
if (ctlObj.Text.IndexOf("页") > 0 )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (ctlObj.Text.IndexOf("共") >= 0)
x += 10;
else
x *= 2;
}
ctlObj.Size = new Size(x,16);
this.Controls.Add(ctlObj);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void lblCtl_Clicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (sender is LinkLabel)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string lblText = ((LinkLabel)sender).Text;
if (lblText == "前一页")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
= start -1;
start = Math.Max(1,end - pageShow + 1);
currPage = end;
}
else if (lblText == "后一页")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
= end + 1;
end = Math.Min(pageCount,start + pageShow -1);
currPage = end;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int page = Convert.ToInt32(lblText);
currPage = page;
}
this.DrawControl();
this.OnLnkLblClicked(e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void ReDraw()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
= pageCount > pageShow;
// currPage = pageCount;
end = pageCount;
start = Math.Max(1,end - pageShow + 1);
this.DrawControl();
this.OnLnkLblClicked(null);
}
}
}