自己写的一个分页控件类(WinForm)
- 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
- }
自己写的一个分页控件类(WinForm)的更多相关文章
- appium+python:自己写的一个滑动控件的方式
#调用方式roll_ele("ID","ele_id","7","up",3)#将控件分为7格,从底部倒数第二格向上滑动 ...
- uinty3d使用ugui封装一个分页控件
我们在显示数据时有的数据比较多,手机内存有限,我们不可能分配很多的控件来显示这些数据,分页是一个不错的选择.比如玩家交易行.我们现在封装一个自己简单的分页控件来显示玩家交易行. 分页控件的原理其实很简 ...
- c#一个分页控件的例子
一.首先下载一个dll,地址:http://pan.baidu.com/share/link?shareid=1628211605&uk=1342867987 二.添加到项目中 三.添加引用 ...
- 小白写的一个ASP.NET分页控件,仅供娱乐
无聊,第一次写博客,自己动手写了一个分页控件.由于我是新手,有很多地方写得不够好,希望各位大牛多多指正.哈哈哈 /// <summary> /// 分页控件 /// </summar ...
- 类似web风格的 Winform 分页控件
背景 最近做一个Winform的小程序,需要用到分页,由于之前一直在用 TonyPagerForWinForm.dll ,但该库没有源代码,网上找的也不全面,索性就准备自己改造一个.在园子里翻了一下, ...
- 自己动手用Javascript写一个无刷新分页控件
.NET技术交流群:337901356 ,欢迎您的加入! 对 于一个用户体验好的网站来说,无刷新技术是很重要的,无刷新,顾名思义,就是局部刷新数据,有用过Asp.net Web Form技术开发网页的 ...
- 纯手写分页控件CSS+JS+SQL
Asp.net中虽然用DataPager配合ListView可以实现分页显示,但是有时候由于开发环境等问题不能用到DataPager控件,那么自己手工写一个分页控件就很有必要了,当然,最重要的是通用性 ...
- 自己写的简单的jQuery分页控件
因为是内部项目,需要分页控件,网上找了一大堆,给领导一看,都说不行,原因很简单,太复杂,领导就想要个简单点的,类似百度的分页,可是自己也没写过Jquery控件,硬着头皮找了些资料,写了这个分页控件,目 ...
- .net分页控件简单实现
.net分页控件简单实现 好久好久没写博客了.....最近写了一个.net的分页控件,放到园子里...你觉得好,就点个赞,不好呢,就告诉我为啥吧.... 是使用Request.QueryString的 ...
随机推荐
- rand值出现负数的解决方案
当rand($number)或者mt_rand($number)的时候,$number太大,超过pow(2,31) - 1;即整型最大值的时候,会出现负数. 如果只是单纯转换成整数而已的话,可以采用s ...
- 架构师书单 2nd Edition--转载
作者:江南白衣,原文出处: http://blog.csdn.net/calvinxiu/archive/2007/03/06/1522032.aspx,转载请保留. 为了2007年的目标,列了下面待 ...
- Maven 修改本地存储库位置--转
step1:默认会放在~/.m2/repository目录下 (“~”代表用户的目录,比如windows下一般都是C:\Documents and Settings\[你的用户名]\), step2: ...
- Redis 客户端连接
Redis 通过监听一个 TCP 端口或者 Unix socket 的方式来接收来自客户端的连接,当一个连接建立后,Redis 内部会进行以下一些操作: 首先,客户端 socket 会被设置为非阻 ...
- vim一个快速切换主题的插件(change-colorscheme,原创)
概述 有时候我们想快速浏览主题并找到一款合适的主题,change-colorscheme将会满足我们的要求. 安装 git https://github.com/chxuan/change-color ...
- 小白日记43:kali渗透测试之Web渗透-SqlMap自动注入(一)-sqlmap参数详解TARGET
SqlMap自动注入(一) sqlmap是一款非常强大的开源sql自动化注入工具,可以用来检测和利用sql注入漏洞[动态页面中get/post参数.cookie.HTTP头].它由Python语言开发 ...
- Android之ListView常用技巧
ListView是一个非常常用的列表控件,虽然在5.x时代ListView的风头正在逐渐的被RecyclerView抢去,但是ListView的使用范围依然十分广泛. 接下来的ListView的常用技 ...
- android开发之路10(文件的读写)
1.安卓中文件的数据存储实例(将文件保存到手机自带存储空间中): ①MainActivity.java public class MainActivity extends Activity imple ...
- 自定义JPA之AttributeConverter
1. 执行类 public class BooleanConverter implements AttributeConverter<Boolean, Integer> { } 2. 属性 ...
- JQUERY 动态时钟
<script type="text/javascript" src="script/jquery-1.8.3.min.js"></scrip ...