自己写的一个分页控件类(WinForm)_.NET_编程开发_程序员俱乐部
自己写的一个分页控件类(WinForm)
- 摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Data;usingSystem.Text;usingSystem.Windows.Forms;namespacexYuanShian.ControlLibrary{///<summary>///翻页控件///</summary>
- 标签:for 一个 自己 控件 winform
class="highlighter-c">
- 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
- {
-
-
-
- public partial class PageControl : UserControl
- {
- #region 私有成员
-
-
-
- private int _PageSize = 50;
-
-
-
- private int _RecordCount = 0;
-
-
-
- private int _PageIndex = 1;
-
-
-
- private int PageCount { get; set; }
-
-
-
- private int GoIndex { get; set; }
- #endregion 私有成员
-
- #region 公有成员
-
-
-
- public int PageSize
- {
- get { return _PageSize; }
- set
- {
- if ( value <= 0 )
- {
- MessageBox.Show( "页码不正确!", "错误!" );
- }
- else
- {
- _PageSize = value;
- this.SetPageCountValue();
- }
- }
- }
-
-
-
- public int RecordCount
- {
- get { return _RecordCount; }
- set
- {
- _RecordCount = value;
- this.SetPageCountValue();
- }
- }
-
-
-
- public int CurrentIndex { get { return _PageIndex; } }
-
-
-
- public event PageControlEventHandler PageChange;
- #endregion 公有成员
-
-
-
- public PageControl()
- {
- InitializeComponent();
- }
-
-
-
-
- public void Init( int RecordCount, int pageSize )
- {
-
- _RecordCount = RecordCount;
-
- _PageSize = pageSize;
-
- GoIndex = 0;
- SetPageCountValue();
- RefreshData();
- }
-
-
-
- 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 设置按钮状态
- }
-
-
-
- private void SetPageCountValue()
- {
-
- PageCount = ( _RecordCount / _PageSize ) + 1;
-
- if ( _RecordCount % _PageSize == 0 ) PageCount--;
-
- if ( _PageIndex > PageCount )
- _PageIndex = PageCount;
- else if ( _PageIndex <= 0 )
- _PageIndex = 1;
- }
-
-
-
-
-
- private void lblfPage_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
- {
-
- this.JumpPage( 1 );
- }
-
-
-
-
-
- private void lblpPage_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
- {
-
- this.JumpPage( _PageIndex - 1 );
- }
-
-
-
-
-
- private void lblnPage_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
- {
-
- this.JumpPage( _PageIndex + 1 );
- }
-
-
-
-
-
- private void lbllPage_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
- {
-
- this.JumpPage( PageCount );
- }
-
-
-
-
-
- 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 );
- }
-
-
-
-
- private void JumpPage( int pIndex )
- {
- _PageIndex = pIndex;
- if ( _PageIndex <= 0 )
- _PageIndex = 1;
- else if ( _PageIndex > PageCount )
- _PageIndex = PageCount;
-
- RefreshData();
- }
-
-
-
- private void RefreshData()
- {
- if ( PageChange != null )
- PageChange( this, new PageControlEventArgs( PageSize, CurrentIndex ) );
- this.RefreshText();
- }
-
-
-
-
-
- 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;
- }
- }
- }
-
-
-
-
-
- private void lblShow_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
- {
- this.RefreshData();
- }
- }
-
-
-
-
-
-
- public delegate void PageControlEventHandler( object Sender, PageControlEventArgs e );
-
-
-
- public class PageControlEventArgs : EventArgs
- {
-
-
-
- public int PageSize = 0;
-
-
-
- public int CurrentIndex = 0;
-
-
-
-
-
- public PageControlEventArgs( int PageSize, int PageIndex )
- {
- this.PageSize = PageSize;
- this.CurrentIndex = PageIndex;
- }
- }
- }
PageControl.Designer.cs类
- namespace xYuanShian.ControlLibrary
- {
- partial class PageControl
- {
-
-
-
- private System.ComponentModel.IContainer components = null;
-
-
-
-
- protected override void Dispose( bool disposing )
- {
- if ( disposing && ( components != null ) )
- {
- components.Dispose();
- }
- base.Dispose( disposing );
- }
-
- #region 组件设计器生成的代码
-
-
-
-
- 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();
-
-
-
- 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 );
-
-
-
- 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 );
-
-
-
- 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 );
-
-
-
- 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 );
-
-
-
- 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;
-
-
-
- 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 );
-
-
-
- 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 );
-
-
-
- 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 = "每页显示记录数";
-
-
-
- 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页";
-
-
-
- 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 );
-
-
-
- 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)
- {
-
- }