zoukankan      html  css  js  c++  java
  • 自己写的一个分页控件类(WinForm)

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Drawing;
    5. using System.Data;
    6. using System.Text;
    7. using System.Windows.Forms;
    8. namespace xYuanShian.ControlLibrary
    9. {
    10.         /// <summary>
    11.         /// 翻页控件
    12.         /// </summary>
    13.         public partial class PageControl : UserControl
    14.         {
    15.                 #region 私有成员
    16.                 /// <summary>
    17.                 /// 页长
    18.                 /// </summary>
    19.                 private int _PageSize = 50;
    20.                 /// <summary>
    21.                 /// 总记录数
    22.                 /// </summary>
    23.                 private int _RecordCount = 0;
    24.                 /// <summary>
    25.                 /// 当前页码
    26.                 /// </summary>
    27.                 private int _PageIndex = 1;
    28.                 /// <summary>
    29.                 /// 获取或设置页数量
    30.                 /// </summary>
    31.                 private int PageCount { get; set; }
    32.                 /// <summary>
    33.                 /// 获取或设置跳转页面
    34.                 /// </summary>
    35.                 private int GoIndex { get; set; }
    36.                 #endregion 私有成员
    37.  
    38.                 #region 公有成员
    39.                 /// <summary>
    40.                 /// 获取或设置页显示数量
    41.                 /// </summary>
    42.                 public int PageSize
    43.                 {
    44.                         get { return _PageSize; }
    45.                         set
    46.                         {
    47.                                 if ( value <= 0 )
    48.                                 {
    49.                                         MessageBox.Show( "页码不正确!", "错误!" );
    50.                                 }
    51.                                 else
    52.                                 {
    53.                                         _PageSize = value;
    54.                                         this.SetPageCountValue();
    55.                                 }
    56.                         }
    57.                 }
    58.                 /// <summary>
    59.                 /// 获取或设置数据总条数
    60.                 /// </summary>
    61.                 public int RecordCount
    62.                 {
    63.                         get { return _RecordCount; }
    64.                         set
    65.                         {
    66.                                 _RecordCount = value;
    67.                                 this.SetPageCountValue();
    68.                         }
    69.                 }
    70.                 /// <summary>
    71.                 /// 获取页码
    72.                 /// </summary>
    73.                 public int CurrentIndex { get { return _PageIndex; } }
    74.                 /// <summary>
    75.                 /// 翻页事件
    76.                 /// </summary>
    77.                 public event PageControlEventHandler PageChange;
    78.                 #endregion 公有成员
    79.                 /// <summary>
    80.                 /// 构造函数
    81.                 /// </summary>
    82.                 public PageControl()
    83.                 {
    84.                         InitializeComponent();
    85.                 }
    86.                 /// <summary>
    87.                 /// 构造函数
    88.                 /// </summary>
    89.                 /// <param name="RecordCount">总记录数</param>
    90.                 public void Init( int RecordCount, int pageSize )
    91.                 {
    92.                         /// 数据条数
    93.                         _RecordCount = RecordCount;
    94.                         /// 页长
    95.                         _PageSize = pageSize;
    96.                         //跳转页码
    97.                         GoIndex = 0;
    98.                         SetPageCountValue();
    99.                         RefreshData();
    100.                 }
    101.                 /// <summary>
    102.                 /// 刷新控件的显示
    103.                 /// </summary>
    104.                 public void RefreshText()
    105.                 {
    106.                         lblCustomInfo.Text = "第 " + CurrentIndex.ToString() + " 页/共 " + PageCount.ToString() + " 页";
    107.                         txtPageSize.Text = _PageSize.ToString();
    108.  
    109.                         #region 设置按钮状态
    110.                         if ( PageCount == 1 )
    111.                         {
    112.                                 lbllPage.Enabled = false;
    113.                                 lblnPage.Enabled = false;
    114.                                 lblpPage.Enabled = false;
    115.                                 lblfPage.Enabled = false;
    116.                         }
    117.                         else if ( _PageIndex >= PageCount )
    118.                         {
    119.                                 lblnPage.Enabled = false;
    120.                                 lbllPage.Enabled = false;
    121.                                 lblpPage.Enabled = true;
    122.                                 lblfPage.Enabled = true;
    123.                         }
    124.                         else if ( _PageIndex <= 1 )
    125.                         {
    126.                                 lblnPage.Enabled = true;
    127.                                 lbllPage.Enabled = true;
    128.                                 lblpPage.Enabled = false;
    129.                                 lblfPage.Enabled = false;
    130.                         }
    131.                         else
    132.                         {
    133.                                 lbllPage.Enabled = true;
    134.                                 lblnPage.Enabled = true;
    135.                                 lblpPage.Enabled = true;
    136.                                 lblfPage.Enabled = true;
    137.                         }
    138.                         #endregion 设置按钮状态
    139.                 }
    140.                 /// <summary>
    141.                 /// 设置各种值
    142.                 /// </summary>
    143.                 private void SetPageCountValue()
    144.                 {
    145.                         /// 页总数
    146.                         PageCount = ( _RecordCount / _PageSize ) + 1;
    147.                         /// 如果总数刚才被页长整除,则页码减1
    148.                         if ( _RecordCount % _PageSize == 0 ) PageCount--;
    149.                         /// 当改页总数改变后,当前页码比最大的页码还大,则设置为最大页码
    150.                         if ( _PageIndex > PageCount )
    151.                                 _PageIndex = PageCount;
    152.                         else if ( _PageIndex <= 0 )
    153.                                 _PageIndex = 1;
    154.                 }
    155.                 /// <summary>
    156.                 /// 翻到首页
    157.                 /// </summary>
    158.                 /// <param name="sender"></param>
    159.                 /// <param name="e"></param>
    160.                 private void lblfPage_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
    161.                 {
    162.                         //第一页
    163.                         this.JumpPage( 1 );
    164.                 }
    165.                 /// <summary>
    166.                 /// 上一页
    167.                 /// </summary>
    168.                 /// <param name="sender"></param>
    169.                 /// <param name="e"></param>
    170.                 private void lblpPage_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
    171.                 {
    172.                         //上一页
    173.                         this.JumpPage( _PageIndex - 1 );
    174.                 }
    175.                 /// <summary>
    176.                 /// 下一页
    177.                 /// </summary>
    178.                 /// <param name="sender"></param>
    179.                 /// <param name="e"></param>
    180.                 private void lblnPage_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
    181.                 {
    182.                         //下一页
    183.                         this.JumpPage( _PageIndex + 1 );
    184.                 }
    185.                 /// <summary>
    186.                 /// 末页
    187.                 /// </summary>
    188.                 /// <param name="sender"></param>
    189.                 /// <param name="e"></param>
    190.                 private void lbllPage_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
    191.                 {
    192.                         //最后页
    193.                         this.JumpPage( PageCount );
    194.                 }
    195.                 /// <summary>
    196.                 /// 跳转
    197.                 /// </summary>
    198.                 /// <param name="sender"></param>
    199.                 /// <param name="e"></param>
    200.                 private void lblGo_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
    201.                 {
    202.                         //跳转
    203.                         int gpi = 0;
    204.                         if ( String.IsNullOrEmpty( tbxGo.Text ) || !int.TryParse( tbxGo.Text, out gpi ) )
    205.                         {
    206.                                 tbxGo.Text = "";
    207.                                 MessageBox.Show( "目标页码不正确!", "错误" );
    208.                                 return;
    209.                         }
    210.                         this.JumpPage( gpi );
    211.                 }
    212.                 /// <summary>
    213.                 /// 翻页
    214.                 /// </summary>
    215.                 /// <param name="pIndex">目标页码</param>
    216.                 private void JumpPage( int pIndex )
    217.                 {
    218.                         _PageIndex = pIndex;
    219.                         if ( _PageIndex <= 0 )
    220.                                 _PageIndex = 1;
    221.                         else if ( _PageIndex > PageCount )
    222.                                 _PageIndex = PageCount;
    223.                         /// 更新外观,以及调用数据
    224.                         RefreshData();
    225.                 }
    226.                 /// <summary>
    227.                 /// 刷新文本
    228.                 /// </summary>
    229.                 private void RefreshData()
    230.                 {
    231.                         if ( PageChange != null )
    232.                                 PageChange( this, new PageControlEventArgs( PageSize, CurrentIndex ) );
    233.                         this.RefreshText();
    234.                 }
    235.                 /// <summary>
    236.                 /// 页长改变
    237.                 /// </summary>
    238.                 /// <param name="sender"></param>
    239.                 /// <param name="e"></param>
    240.                 private void txtPageSize_TextChanged( object sender, EventArgs e )
    241.                 {
    242.                         if ( !String.IsNullOrEmpty( txtPageSize.Text ) )
    243.                         {
    244.                                 int ps = 0;
    245.                                 if ( int.TryParse( txtPageSize.Text, out ps ) )
    246.                                 {
    247.                                         PageSize = ps;
    248.                                 }
    249.                         }
    250.                 }
    251.                 /// <summary>
    252.                 /// 改变页长
    253.                 /// </summary>
    254.                 /// <param name="sender"></param>
    255.                 /// <param name="e"></param>
    256.                 private void lblShow_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
    257.                 {
    258.                         this.RefreshData();
    259.                 }
    260.         }
    261.         /// <summary>
    262.         /// 翻页事件
    263.         /// </summary>
    264.         /// <param name="Sender">事件调用者</param>
    265.         /// <param name="PageSize">页长</param>
    266.         /// <param name="PageIndex">页码</param>
    267.         public delegate void PageControlEventHandler( object Sender, PageControlEventArgs e );
    268.         /// <summary>
    269.         /// 翻页事件参数
    270.         /// </summary>
    271.         public class PageControlEventArgs : EventArgs
    272.         {
    273.                 /// <summary>
    274.                 /// 页长
    275.                 /// </summary>
    276.                 public int PageSize = 0;
    277.                 /// <summary>
    278.                 /// 页码
    279.                 /// </summary>
    280.                 public int CurrentIndex = 0;
    281.                 /// <summary>
    282.                 /// 构造函数
    283.                 /// </summary>
    284.                 /// <param name="PageSize"></param>
    285.                 /// <param name="PageIndex"></param>
    286.                 public PageControlEventArgs( int PageSize, int PageIndex )
    287.                 {
    288.                         this.PageSize = PageSize;
    289.                         this.CurrentIndex = PageIndex;
    290.                 }
    291.         }
    292. }

    PageControl.Designer.cs类

     

    1. namespace xYuanShian.ControlLibrary
    2. {
    3.         partial class PageControl
    4.         {
    5.                 /// <summary>
    6.                 /// 必需的设计器变量。
    7.                 /// </summary>
    8.                 private System.ComponentModel.IContainer components = null;
    9.                 /// <summary>
    10.                 /// 清理所有正在使用的资源。
    11.                 /// </summary>
    12.                 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    13.                 protected override void Dispose( bool disposing )
    14.                 {
    15.                         if ( disposing && ( components != null ) )
    16.                         {
    17.                                 components.Dispose();
    18.                         }
    19.                         base.Dispose( disposing );
    20.                 }
    21.  
    22.                 #region 组件设计器生成的代码
    23.                 /// <summary>
    24.                 /// 设计器支持所需的方法 - 不要
    25.                 /// 使用代码编辑器修改此方法的内容。
    26.                 /// </summary>
    27.                 private void InitializeComponent()
    28.                 {
    29.                         this.lblfPage = new System.Windows.Forms.LinkLabel();
    30.                         this.lblpPage = new System.Windows.Forms.LinkLabel();
    31.                         this.lblnPage = new System.Windows.Forms.LinkLabel();
    32.                         this.lbllPage = new System.Windows.Forms.LinkLabel();
    33.                         this.tbxGo = new System.Windows.Forms.TextBox();
    34.                         this.lblGo = new System.Windows.Forms.LinkLabel();
    35.                         this.txtPageSize = new System.Windows.Forms.TextBox();
    36.                         this.label6 = new System.Windows.Forms.Label();
    37.                         this.lblCustomInfo = new System.Windows.Forms.Label();
    38.                         this.lblShow = new System.Windows.Forms.LinkLabel();
    39.                         this.SuspendLayout();
    40.                         // 
    41.                         // lblfPage
    42.                         // 
    43.                         this.lblfPage.AutoSize = true;
    44.                         this.lblfPage.Font = new System.Drawing.Font( "宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte )( 134 ) ) );
    45.                         this.lblfPage.LinkColor = System.Drawing.Color.Black;
    46.                         this.lblfPage.Location = new System.Drawing.Point( 6, 10 );
    47.                         this.lblfPage.Name = "lblfPage";
    48.                         this.lblfPage.Size = new System.Drawing.Size( 29, 12 );
    49.                         this.lblfPage.TabIndex = 0;
    50.                         this.lblfPage.TabStop = true;
    51.                         this.lblfPage.Text = "首页";
    52.                         this.lblfPage.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler( this.lblfPage_LinkClicked );
    53.                         // 
    54.                         // lblpPage
    55.                         // 
    56.                         this.lblpPage.AutoSize = true;
    57.                         this.lblpPage.Font = new System.Drawing.Font( "宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte )( 134 ) ) );
    58.                         this.lblpPage.LinkColor = System.Drawing.Color.Black;
    59.                         this.lblpPage.Location = new System.Drawing.Point( 41, 10 );
    60.                         this.lblpPage.Name = "lblpPage";
    61.                         this.lblpPage.Size = new System.Drawing.Size( 29, 12 );
    62.                         this.lblpPage.TabIndex = 1;
    63.                         this.lblpPage.TabStop = true;
    64.                         this.lblpPage.Text = "上页";
    65.                         this.lblpPage.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler( this.lblpPage_LinkClicked );
    66.                         // 
    67.                         // lblnPage
    68.                         // 
    69.                         this.lblnPage.AutoSize = true;
    70.                         this.lblnPage.Font = new System.Drawing.Font( "宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte )( 134 ) ) );
    71.                         this.lblnPage.LinkColor = System.Drawing.Color.Black;
    72.                         this.lblnPage.Location = new System.Drawing.Point( 76, 10 );
    73.                         this.lblnPage.Name = "lblnPage";
    74.                         this.lblnPage.Size = new System.Drawing.Size( 29, 12 );
    75.                         this.lblnPage.TabIndex = 2;
    76.                         this.lblnPage.TabStop = true;
    77.                         this.lblnPage.Text = "下页";
    78.                         this.lblnPage.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler( this.lblnPage_LinkClicked );
    79.                         // 
    80.                         // lbllPage
    81.                         // 
    82.                         this.lbllPage.AutoSize = true;
    83.                         this.lbllPage.Font = new System.Drawing.Font( "宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte )( 134 ) ) );
    84.                         this.lbllPage.LinkColor = System.Drawing.Color.Black;
    85.                         this.lbllPage.Location = new System.Drawing.Point( 111, 10 );
    86.                         this.lbllPage.Name = "lbllPage";
    87.                         this.lbllPage.Size = new System.Drawing.Size( 29, 12 );
    88.                         this.lbllPage.TabIndex = 3;
    89.                         this.lbllPage.TabStop = true;
    90.                         this.lbllPage.Text = "末页";
    91.                         this.lbllPage.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler( this.lbllPage_LinkClicked );
    92.                         // 
    93.                         // tbxGo
    94.                         // 
    95.                         this.tbxGo.Location = new System.Drawing.Point( 326, 7 );
    96.                         this.tbxGo.Name = "tbxGo";
    97.                         this.tbxGo.Size = new System.Drawing.Size( 39, 21 );
    98.                         this.tbxGo.TabIndex = 5;
    99.                         // 
    100.                         // lblGo
    101.                         // 
    102.                         this.lblGo.AutoSize = true;
    103.                         this.lblGo.Font = new System.Drawing.Font( "宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte )( 134 ) ) );
    104.                         this.lblGo.LinkColor = System.Drawing.Color.Black;
    105.                         this.lblGo.Location = new System.Drawing.Point( 371, 10 );
    106.                         this.lblGo.Name = "lblGo";
    107.                         this.lblGo.Size = new System.Drawing.Size( 29, 12 );
    108.                         this.lblGo.TabIndex = 6;
    109.                         this.lblGo.TabStop = true;
    110.                         this.lblGo.Text = "跳转";
    111.                         this.lblGo.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler( this.lblGo_LinkClicked );
    112.                         // 
    113.                         // txtPageSize
    114.                         // 
    115.                         this.txtPageSize.Location = new System.Drawing.Point( 234, 7 );
    116.                         this.txtPageSize.Name = "txtPageSize";
    117.                         this.txtPageSize.Size = new System.Drawing.Size( 40, 21 );
    118.                         this.txtPageSize.TabIndex = 15;
    119.                         this.txtPageSize.TextChanged += new System.EventHandler( this.txtPageSize_TextChanged );
    120.                         // 
    121.                         // label6
    122.                         // 
    123.                         this.label6.AutoSize = true;
    124.                         this.label6.Location = new System.Drawing.Point( 146, 11 );
    125.                         this.label6.Name = "label6";
    126.                         this.label6.Size = new System.Drawing.Size( 89, 12 );
    127.                         this.label6.TabIndex = 14;
    128.                         this.label6.Text = "每页显示记录数";
    129.                         // 
    130.                         // lblCustomInfo
    131.                         // 
    132.                         this.lblCustomInfo.AutoSize = true;
    133.                         this.lblCustomInfo.Location = new System.Drawing.Point( 406, 10 );
    134.                         this.lblCustomInfo.Name = "lblCustomInfo";
    135.                         this.lblCustomInfo.Size = new System.Drawing.Size( 35, 12 );
    136.                         this.lblCustomInfo.TabIndex = 16;
    137.                         this.lblCustomInfo.Text = "第1页";
    138.                         // 
    139.                         // lblShow
    140.                         // 
    141.                         this.lblShow.AutoSize = true;
    142.                         this.lblShow.Font = new System.Drawing.Font( "宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte )( 134 ) ) );
    143.                         this.lblShow.LinkColor = System.Drawing.Color.Black;
    144.                         this.lblShow.Location = new System.Drawing.Point( 278, 11 );
    145.                         this.lblShow.Name = "lblShow";
    146.                         this.lblShow.Size = new System.Drawing.Size( 29, 12 );
    147.                         this.lblShow.TabIndex = 17;
    148.                         this.lblShow.TabStop = true;
    149.                         this.lblShow.Text = "显示";
    150.                         this.lblShow.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler( this.lblShow_LinkClicked );
    151.                         // 
    152.                         // PageControl
    153.                         // 
    154.                         this.AutoScaleDimensions = new System.Drawing.SizeF( 6F, 12F );
    155.                         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    156.                         this.Controls.Add( this.lblShow );
    157.                         this.Controls.Add( this.lblCustomInfo );
    158.                         this.Controls.Add( this.txtPageSize );
    159.                         this.Controls.Add( this.label6 );
    160.                         this.Controls.Add( this.lblGo );
    161.                         this.Controls.Add( this.tbxGo );
    162.                         this.Controls.Add( this.lbllPage );
    163.                         this.Controls.Add( this.lblnPage );
    164.                         this.Controls.Add( this.lblpPage );
    165.                         this.Controls.Add( this.lblfPage );
    166.                         this.Name = "PageControl";
    167.                         this.Size = new System.Drawing.Size( 553, 30 );
    168.                         this.ResumeLayout( false );
    169.                         this.PerformLayout();
    170.                 }
    171.  
    172.                 #endregion
    173.                 private System.Windows.Forms.LinkLabel lblfPage;
    174.                 private System.Windows.Forms.LinkLabel lblpPage;
    175.                 private System.Windows.Forms.LinkLabel lblnPage;
    176.                 private System.Windows.Forms.LinkLabel lbllPage;
    177.                 private System.Windows.Forms.TextBox tbxGo;
    178.                 private System.Windows.Forms.LinkLabel lblGo;
    179.                 private System.Windows.Forms.TextBox txtPageSize;
    180.                 private System.Windows.Forms.Label label6;
    181.                 private System.Windows.Forms.Label lblCustomInfo;
    182.                 private System.Windows.Forms.LinkLabel lblShow;
    183.         }
    184. }

    使用方法

    1.                 private void test()
    2.                 {
    3.                         PageControl pageControl1 = new PageControl();
    4.                         pageControl1.Init( 总记录数, 页长 );          // 或者用下面的方式
    5.                         pageControl1.RecordCount = 设置记录数;
    6.                         pageControl1.PageSize = 页长;
    7.                         pageControl1.PageChange += new PageControlEventHandler( myDataBinder );
    8.                 }
    9.                 private void myDataBinder(object Sender, PageControlEventArgs e)
    10.                 {
    11.                         //绑定数据源操作 e.CurrentIndex, e.PageSize
    12.                 }
  • 相关阅读:
    LeetCode 88. Merge Sorted Array
    LeetCode 75. Sort Colors
    LeetCode 581. Shortest Unsorted Continuous Subarray
    LeetCode 20. Valid Parentheses
    LeetCode 53. Maximum Subarray
    LeetCode 461. Hamming Distance
    LeetCode 448. Find All Numbers Disappeared in an Array
    LeetCode 976. Largest Perimeter Triangle
    LeetCode 1295. Find Numbers with Even Number of Digits
    如何自学并且系统学习计算机网络?(知乎问答)
  • 原文地址:https://www.cnblogs.com/rr163/p/3955938.html
Copyright © 2011-2022 走看看