- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Text;
- using System.Windows.Forms;
- namespace xYuanShian.ControlLibrary
- {
- /// <summary>
- /// 翻页控件
- /// </summary>
- public partial class PageControl : UserControl
- {
- #region 私有成员
- /// <summary>
- /// 页长
- /// </summary>
- private int _PageSize = 50;
- /// <summary>
- /// 总记录数
- /// </summary>
- private int _RecordCount = 0;
- /// <summary>
- /// 当前页码
- /// </summary>
- private int _PageIndex = 1;
- /// <summary>
- /// 获取或设置页数量
- /// </summary>
- private int PageCount { get; set; }
- /// <summary>
- /// 获取或设置跳转页面
- /// </summary>
- private int GoIndex { get; set; }
- #endregion 私有成员
- #region 公有成员
- /// <summary>
- /// 获取或设置页显示数量
- /// </summary>
- public int PageSize
- {
- get { return _PageSize; }
- set
- {
- if ( value <= 0 )
- {
- MessageBox.Show( "页码不正确!", "错误!" );
- }
- else
- {
- _PageSize = value;
- this.SetPageCountValue();
- }
- }
- }
- /// <summary>
- /// 获取或设置数据总条数
- /// </summary>
- public int RecordCount
- {
- get { return _RecordCount; }
- set
- {
- _RecordCount = value;
- this.SetPageCountValue();
- }
- }
- /// <summary>
- /// 获取页码
- /// </summary>
- public int CurrentIndex { get { return _PageIndex; } }
- /// <summary>
- /// 翻页事件
- /// </summary>
- public event PageControlEventHandler PageChange;
- #endregion 公有成员
- /// <summary>
- /// 构造函数
- /// </summary>
- public PageControl()
- {
- InitializeComponent();
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="RecordCount">总记录数</param>
- public void Init( int RecordCount, int pageSize )
- {
- /// 数据条数
- _RecordCount = RecordCount;
- /// 页长
- _PageSize = pageSize;
- //跳转页码
- GoIndex = 0;
- SetPageCountValue();
- RefreshData();
- }
- /// <summary>
- /// 刷新控件的显示
- /// </summary>
- public void RefreshText()
- {
- lblCustomInfo.Text = "第 " + CurrentIndex.ToString() + " 页/共 " + PageCount.ToString() + " 页";
- txtPageSize.Text = _PageSize.ToString();
- #region 设置按钮状态
- if ( PageCount == 1 )
- {
- lbllPage.Enabled = false;
- lblnPage.Enabled = false;
- lblpPage.Enabled = false;
- lblfPage.Enabled = false;
- }
- else if ( _PageIndex >= PageCount )
- {
- lblnPage.Enabled = false;
- lbllPage.Enabled = false;
- lblpPage.Enabled = true;
- lblfPage.Enabled = true;
- }
- else if ( _PageIndex <= 1 )
- {
- lblnPage.Enabled = true;
- lbllPage.Enabled = true;
- lblpPage.Enabled = false;
- lblfPage.Enabled = false;
- }
- else
- {
- lbllPage.Enabled = true;
- lblnPage.Enabled = true;
- lblpPage.Enabled = true;
- lblfPage.Enabled = true;
- }
- #endregion 设置按钮状态
- }
- /// <summary>
- /// 设置各种值
- /// </summary>
- private void SetPageCountValue()
- {
- /// 页总数
- PageCount = ( _RecordCount / _PageSize ) + 1;
- /// 如果总数刚才被页长整除,则页码减1
- if ( _RecordCount % _PageSize == 0 ) PageCount--;
- /// 当改页总数改变后,当前页码比最大的页码还大,则设置为最大页码
- if ( _PageIndex > PageCount )
- _PageIndex = PageCount;
- else if ( _PageIndex <= 0 )
- _PageIndex = 1;
- }
- /// <summary>
- /// 翻到首页
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void lblfPage_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
- {
- //第一页
- this.JumpPage( 1 );
- }
- /// <summary>
- /// 上一页
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void lblpPage_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
- {
- //上一页
- this.JumpPage( _PageIndex - 1 );
- }
- /// <summary>
- /// 下一页
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void lblnPage_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
- {
- //下一页
- this.JumpPage( _PageIndex + 1 );
- }
- /// <summary>
- /// 末页
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void lbllPage_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
- {
- //最后页
- this.JumpPage( PageCount );
- }
- /// <summary>
- /// 跳转
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void lblGo_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
- {
- //跳转
- int gpi = 0;
- if ( String.IsNullOrEmpty( tbxGo.Text ) || !int.TryParse( tbxGo.Text, out gpi ) )
- {
- tbxGo.Text = "";
- MessageBox.Show( "目标页码不正确!", "错误" );
- return;
- }
- this.JumpPage( gpi );
- }
- /// <summary>
- /// 翻页
- /// </summary>
- /// <param name="pIndex">目标页码</param>
- private void JumpPage( int pIndex )
- {
- _PageIndex = pIndex;
- if ( _PageIndex <= 0 )
- _PageIndex = 1;
- else if ( _PageIndex > PageCount )
- _PageIndex = PageCount;
- /// 更新外观,以及调用数据
- RefreshData();
- }
- /// <summary>
- /// 刷新文本
- /// </summary>
- private void RefreshData()
- {
- if ( PageChange != null )
- PageChange( this, new PageControlEventArgs( PageSize, CurrentIndex ) );
- this.RefreshText();
- }
- /// <summary>
- /// 页长改变
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void txtPageSize_TextChanged( object sender, EventArgs e )
- {
- if ( !String.IsNullOrEmpty( txtPageSize.Text ) )
- {
- int ps = 0;
- if ( int.TryParse( txtPageSize.Text, out ps ) )
- {
- PageSize = ps;
- }
- }
- }
- /// <summary>
- /// 改变页长
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void lblShow_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
- {
- this.RefreshData();
- }
- }
- /// <summary>
- /// 翻页事件
- /// </summary>
- /// <param name="Sender">事件调用者</param>
- /// <param name="PageSize">页长</param>
- /// <param name="PageIndex">页码</param>
- public delegate void PageControlEventHandler( object Sender, PageControlEventArgs e );
- /// <summary>
- /// 翻页事件参数
- /// </summary>
- public class PageControlEventArgs : EventArgs
- {
- /// <summary>
- /// 页长
- /// </summary>
- public int PageSize = 0;
- /// <summary>
- /// 页码
- /// </summary>
- public int CurrentIndex = 0;
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="PageSize"></param>
- /// <param name="PageIndex"></param>
- public PageControlEventArgs( int PageSize, int PageIndex )
- {
- this.PageSize = PageSize;
- this.CurrentIndex = PageIndex;
- }
- }
- }
PageControl.Designer.cs类
- namespace xYuanShian.ControlLibrary
- {
- partial class PageControl
- {
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
- protected override void Dispose( bool disposing )
- {
- if ( disposing && ( components != null ) )
- {
- components.Dispose();
- }
- base.Dispose( disposing );
- }
- #region 组件设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要
- /// 使用代码编辑器修改此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.lblfPage = new System.Windows.Forms.LinkLabel();
- this.lblpPage = new System.Windows.Forms.LinkLabel();
- this.lblnPage = new System.Windows.Forms.LinkLabel();
- this.lbllPage = new System.Windows.Forms.LinkLabel();
- this.tbxGo = new System.Windows.Forms.TextBox();
- this.lblGo = new System.Windows.Forms.LinkLabel();
- this.txtPageSize = new System.Windows.Forms.TextBox();
- this.label6 = new System.Windows.Forms.Label();
- this.lblCustomInfo = new System.Windows.Forms.Label();
- this.lblShow = new System.Windows.Forms.LinkLabel();
- this.SuspendLayout();
- //
- // lblfPage
- //
- this.lblfPage.AutoSize = true;
- this.lblfPage.Font = new System.Drawing.Font( "宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte )( 134 ) ) );
- this.lblfPage.LinkColor = System.Drawing.Color.Black;
- this.lblfPage.Location = new System.Drawing.Point( 6, 10 );
- this.lblfPage.Name = "lblfPage";
- this.lblfPage.Size = new System.Drawing.Size( 29, 12 );
- this.lblfPage.TabIndex = 0;
- this.lblfPage.TabStop = true;
- this.lblfPage.Text = "首页";
- this.lblfPage.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler( this.lblfPage_LinkClicked );
- //
- // lblpPage
- //
- this.lblpPage.AutoSize = true;
- this.lblpPage.Font = new System.Drawing.Font( "宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte )( 134 ) ) );
- this.lblpPage.LinkColor = System.Drawing.Color.Black;
- this.lblpPage.Location = new System.Drawing.Point( 41, 10 );
- this.lblpPage.Name = "lblpPage";
- this.lblpPage.Size = new System.Drawing.Size( 29, 12 );
- this.lblpPage.TabIndex = 1;
- this.lblpPage.TabStop = true;
- this.lblpPage.Text = "上页";
- this.lblpPage.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler( this.lblpPage_LinkClicked );
- //
- // lblnPage
- //
- this.lblnPage.AutoSize = true;
- this.lblnPage.Font = new System.Drawing.Font( "宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte )( 134 ) ) );
- this.lblnPage.LinkColor = System.Drawing.Color.Black;
- this.lblnPage.Location = new System.Drawing.Point( 76, 10 );
- this.lblnPage.Name = "lblnPage";
- this.lblnPage.Size = new System.Drawing.Size( 29, 12 );
- this.lblnPage.TabIndex = 2;
- this.lblnPage.TabStop = true;
- this.lblnPage.Text = "下页";
- this.lblnPage.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler( this.lblnPage_LinkClicked );
- //
- // lbllPage
- //
- this.lbllPage.AutoSize = true;
- this.lbllPage.Font = new System.Drawing.Font( "宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte )( 134 ) ) );
- this.lbllPage.LinkColor = System.Drawing.Color.Black;
- this.lbllPage.Location = new System.Drawing.Point( 111, 10 );
- this.lbllPage.Name = "lbllPage";
- this.lbllPage.Size = new System.Drawing.Size( 29, 12 );
- this.lbllPage.TabIndex = 3;
- this.lbllPage.TabStop = true;
- this.lbllPage.Text = "末页";
- this.lbllPage.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler( this.lbllPage_LinkClicked );
- //
- // tbxGo
- //
- this.tbxGo.Location = new System.Drawing.Point( 326, 7 );
- this.tbxGo.Name = "tbxGo";
- this.tbxGo.Size = new System.Drawing.Size( 39, 21 );
- this.tbxGo.TabIndex = 5;
- //
- // lblGo
- //
- this.lblGo.AutoSize = true;
- this.lblGo.Font = new System.Drawing.Font( "宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte )( 134 ) ) );
- this.lblGo.LinkColor = System.Drawing.Color.Black;
- this.lblGo.Location = new System.Drawing.Point( 371, 10 );
- this.lblGo.Name = "lblGo";
- this.lblGo.Size = new System.Drawing.Size( 29, 12 );
- this.lblGo.TabIndex = 6;
- this.lblGo.TabStop = true;
- this.lblGo.Text = "跳转";
- this.lblGo.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler( this.lblGo_LinkClicked );
- //
- // txtPageSize
- //
- this.txtPageSize.Location = new System.Drawing.Point( 234, 7 );
- this.txtPageSize.Name = "txtPageSize";
- this.txtPageSize.Size = new System.Drawing.Size( 40, 21 );
- this.txtPageSize.TabIndex = 15;
- this.txtPageSize.TextChanged += new System.EventHandler( this.txtPageSize_TextChanged );
- //
- // label6
- //
- this.label6.AutoSize = true;
- this.label6.Location = new System.Drawing.Point( 146, 11 );
- this.label6.Name = "label6";
- this.label6.Size = new System.Drawing.Size( 89, 12 );
- this.label6.TabIndex = 14;
- this.label6.Text = "每页显示记录数";
- //
- // lblCustomInfo
- //
- this.lblCustomInfo.AutoSize = true;
- this.lblCustomInfo.Location = new System.Drawing.Point( 406, 10 );
- this.lblCustomInfo.Name = "lblCustomInfo";
- this.lblCustomInfo.Size = new System.Drawing.Size( 35, 12 );
- this.lblCustomInfo.TabIndex = 16;
- this.lblCustomInfo.Text = "第1页";
- //
- // lblShow
- //
- this.lblShow.AutoSize = true;
- this.lblShow.Font = new System.Drawing.Font( "宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte )( 134 ) ) );
- this.lblShow.LinkColor = System.Drawing.Color.Black;
- this.lblShow.Location = new System.Drawing.Point( 278, 11 );
- this.lblShow.Name = "lblShow";
- this.lblShow.Size = new System.Drawing.Size( 29, 12 );
- this.lblShow.TabIndex = 17;
- this.lblShow.TabStop = true;
- this.lblShow.Text = "显示";
- this.lblShow.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler( this.lblShow_LinkClicked );
- //
- // PageControl
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF( 6F, 12F );
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.Controls.Add( this.lblShow );
- this.Controls.Add( this.lblCustomInfo );
- this.Controls.Add( this.txtPageSize );
- this.Controls.Add( this.label6 );
- this.Controls.Add( this.lblGo );
- this.Controls.Add( this.tbxGo );
- this.Controls.Add( this.lbllPage );
- this.Controls.Add( this.lblnPage );
- this.Controls.Add( this.lblpPage );
- this.Controls.Add( this.lblfPage );
- this.Name = "PageControl";
- this.Size = new System.Drawing.Size( 553, 30 );
- this.ResumeLayout( false );
- this.PerformLayout();
- }
- #endregion
- private System.Windows.Forms.LinkLabel lblfPage;
- private System.Windows.Forms.LinkLabel lblpPage;
- private System.Windows.Forms.LinkLabel lblnPage;
- private System.Windows.Forms.LinkLabel lbllPage;
- private System.Windows.Forms.TextBox tbxGo;
- private System.Windows.Forms.LinkLabel lblGo;
- private System.Windows.Forms.TextBox txtPageSize;
- private System.Windows.Forms.Label label6;
- private System.Windows.Forms.Label lblCustomInfo;
- private System.Windows.Forms.LinkLabel lblShow;
- }
- }
使用方法
- private void test()
- {
- PageControl pageControl1 = new PageControl();
- pageControl1.Init( 总记录数, 页长 ); // 或者用下面的方式
- pageControl1.RecordCount = 设置记录数;
- pageControl1.PageSize = 页长;
- pageControl1.PageChange += new PageControlEventHandler( myDataBinder );
- }
- private void myDataBinder(object Sender, PageControlEventArgs e)
- {
- //绑定数据源操作 e.CurrentIndex, e.PageSize
- }