【NET】Winform分页控件初探

时间:2023-12-04 16:42:02
 public partial class WinFormPager : UserControl
{
const int PAGE_INDEX_START = ;
/// <summary>
/// 当前页
/// </summary>
public int CurrentPage
{
get;
set;
}
/// <summary>
/// 每页显示条数
/// </summary>
[Description("每页显示条数"), Category("分页设置"), DefaultValue()]
public int PageSize
{
get;
set;
}
/// <summary>
/// 总共多少页
/// </summary>
public int PageTotal
{
get;
set;
}
int recordCount;
public int RecordCount
{
get {
return recordCount;
}
set {
recordCount = value;
SetBtnPrePageAndBtnNextPage();
}
} private Label lbMsg;
private LinkLabel llFirst;
private LinkLabel llPrev;
private LinkLabel llNext;
private LinkLabel llLast;//按钮间距离 public delegate void PageChangeDelegate();
/// <summary>
/// 当前页改变时发生的事件
/// </summary>
[Description("当前页改变时发生的事件"), Category("分页设置")]
public event PageChangeDelegate PageChanged;
public WinFormPager()
{
InitializeComponent();
} /// <summary>
/// 设置上一页、下一页是否可用以及当前页按钮字体颜色
/// </summary>
public void SetBtnPrePageAndBtnNextPage()
{ if (recordCount == )
{
llFirst.Enabled = llPrev.Enabled = llNext.Enabled = llLast.Enabled = false;
}
else
{
PageTotal = Convert.ToInt32(Math.Ceiling(RecordCount * 1.0 / PageSize));//总共多少页
if (CurrentPage == PAGE_INDEX_START)
{
llFirst.Enabled = llPrev.Enabled = false;
}
else
{
llFirst.Enabled = llPrev.Enabled = true;
} if (CurrentPage == PageTotal)
{
llNext.Enabled = llLast.Enabled = false;
}
else
{
llNext.Enabled = llLast.Enabled = true;
}
}
this.lbMsg.Text = string.Format(this.lbMsg.Tag.ToString(), RecordCount, PageSize, CurrentPage, PageTotal);
} private void InitializeComponent()
{
this.lbMsg = new System.Windows.Forms.Label();
this.llFirst = new System.Windows.Forms.LinkLabel();
this.llPrev = new System.Windows.Forms.LinkLabel();
this.llNext = new System.Windows.Forms.LinkLabel();
this.llLast = new System.Windows.Forms.LinkLabel();
this.SuspendLayout();
//
// lbMsg
//
this.lbMsg.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.lbMsg.AutoSize = true;
this.lbMsg.Location = new System.Drawing.Point(, );
this.lbMsg.Name = "lbMsg";
this.lbMsg.Size = new System.Drawing.Size(, );
this.lbMsg.TabIndex = ;
this.lbMsg.Tag = "共{0}条 {1}条/页 当前第{2}页/{3}页";
this.lbMsg.Text = "共{0}条 {1}条/页 当前第{2}页/{3}页";
this.lbMsg.UseCompatibleTextRendering = true;
//
// llFirst
//
this.llFirst.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.llFirst.AutoSize = true;
this.llFirst.Location = new System.Drawing.Point(, );
this.llFirst.Name = "llFirst";
this.llFirst.Size = new System.Drawing.Size(, );
this.llFirst.TabIndex = ;
this.llFirst.TabStop = true;
this.llFirst.Text = "首页";
this.llFirst.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.llFirst_LinkClicked);
//
// llPrev
//
this.llPrev.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.llPrev.AutoSize = true;
this.llPrev.Location = new System.Drawing.Point(, );
this.llPrev.Name = "llPrev";
this.llPrev.Size = new System.Drawing.Size(, );
this.llPrev.TabIndex = ;
this.llPrev.TabStop = true;
this.llPrev.Text = "上一页";
this.llPrev.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.llPrev_LinkClicked);
//
// llNext
//
this.llNext.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.llNext.AutoSize = true;
this.llNext.Location = new System.Drawing.Point(, );
this.llNext.Name = "llNext";
this.llNext.Size = new System.Drawing.Size(, );
this.llNext.TabIndex = ;
this.llNext.TabStop = true;
this.llNext.Text = "下一页";
this.llNext.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.llNext_LinkClicked);
//
// llLast
//
this.llLast.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.llLast.AutoSize = true;
this.llLast.Location = new System.Drawing.Point(, );
this.llLast.Name = "llLast";
this.llLast.Size = new System.Drawing.Size(, );
this.llLast.TabIndex = ;
this.llLast.TabStop = true;
this.llLast.Text = "末页";
this.llLast.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.llLast_LinkClicked);
//
// WinFormPager
//
this.Controls.Add(this.llLast);
this.Controls.Add(this.llNext);
this.Controls.Add(this.llPrev);
this.Controls.Add(this.llFirst);
this.Controls.Add(this.lbMsg);
this.Name = "WinFormPager";
this.Size = new System.Drawing.Size(, );
this.ResumeLayout(false);
this.PerformLayout(); } private void llFirst_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
PageAction(PAGE_INDEX_START);
SetBtnPrePageAndBtnNextPage();
} public void PageAction(int pageNumOperation) {
CurrentPage = pageNumOperation;
if (CurrentPage < PAGE_INDEX_START)
{
CurrentPage = PAGE_INDEX_START;
}
if (CurrentPage > PageTotal) {
CurrentPage = PageTotal;
} if (PageChanged != null)
{
PageChanged();
}
} private void llLast_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
PageAction(PageTotal);
} private void llPrev_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
PageAction(CurrentPage-);
} private void llNext_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
PageAction(CurrentPage + );
} }

调用

public ListViewExtendPager()
{//分页List 控件(此控件包含分页控件)构造函数
InitializeComponent();
this.wfpPager.PageSize = ;
this.wfpPager.PageChanged += new WinFormPager.PageChangeDelegate(
() =>
{
DataBind();
}
);
}
public Action<ListViewExtendPager> dataBindAction;
private void ListViewExtendPager_Load(object sender, EventArgs e)
{
this.wfpPager.CurrentPage = ;
DataBind();
} private void DataBind() {
if (dataBindAction != null) {
dataBindAction(this);
}
}