Windows Forms 分页控件

开发WinForm项目中自己写的一个分页控件,没有什么技术含量,不过自己感觉还是蛮好用的,拿出来和大家分享一下。
一、使用到的WinForm控件有连个toolStrip和一个imageList, imageList主要负责保存四个翻页的小图片,


二、分页控件工作原理
      1.首先让控件继承UserControl, INotifyPropertyChanged,继承INotifyPropertyChanged,主要是实现当用户点击翻页或者更改文本框里的数字,那么当前列表自动更新信息,如果要做成点击按钮翻页可以直接写个委托来定义一个事件就可以了,这里这么实现也是为了操作方便。
     实现代码:
    
     public   partial   class  WinFromPager : UserControl, INotifyPropertyChanged
    {
    }
      2. 定义事件模型,实际上时定义一个在列表页面执行翻页的事件
  public   event  PropertyChangedEventHandler PropertyChanged;

        
protected   virtual   void  OnPropertyChanged( string  propertyName)
        {
            
if  (PropertyChanged  !=   null )
            {
                PropertyChanged(
this new  PropertyChangedEventArgs(propertyName));
            }
        }
      3. 定义属性
        private   int  _mixPage  =   1 ;
        
private   int  _maxPage  =   1 ;
        
private   int  _currentPage  =   1 ;
        
private   int  _RecordCount  =   0 ;
        
private   int  _pageSize  =   20 ;
        
private   string  _lblPageInfo  =   "  当前页({0})/总共({1})页,共({2})条记录,每页({3})条 " ;
         ///   <sumary>
        
///  第一页索引值
        
///   </summary>
         public   int  MixPage
        {
            
get
            {
                
return  _mixPage;
            }
            
set
            {
                _mixPage 
=  value;
            }
        }

        
///   <summary>
        
///  总页数
        
///   </summary>
         public   int  MaxPage
        {
            
get
            {
                
return  _maxPage;
            }
            
set
            {
                _maxPage 
=  value;
            }
        }


        
///   <summary>
        
///  当前页索引
        
///   </summary>
         public   int  CurrentPage
        {
            
get
            {
                
return   this ._currentPage;
            }
            
set
            {
                
this ._currentPage  =  value;
            }
        }

        
///   <summary>
        
///  每页显示记录数
        
///   </summary>
         public   int  PageSize
        {
            
get
            {
                
return   this ._pageSize;
            }
            
set
            {
                
this ._pageSize  =  value;
            }
        }

        
///   <summary>
        
///  开始索引
        
///   </summary>
         public   int  StartIndex
        {
            
get
            {
                
return  ((CurrentPage  -   1 *  PageSize)  +   1 ;
            }
        }

        
///   <summary>
        
///  结束索引
        
///   </summary>
         public   int  EndIndex
        {
            
get
            {
                
if  ((RecordCount  -  ((CurrentPage  -   1 *  PageSize))  >=  PageSize)
                {
                    
return CurrentPage *  PageSize ;
                }
                
else
                {
                    
return  RecordCount;
                }
            }
        }

        
///   <summary>
        
///  总记录
        
///   </summary>
         public   int  RecordCount
        {
            
set
            {
                _RecordCount 
=  value;
                GetPageCount();
            }
            
get
            {
                
return  _RecordCount;
            }
        }

        
///   <summary>
        
///  查询当前索引页
        
///   </summary>
         public   int  CurrentIndex
        {
            
set
            {
                
this ._currentPage  =  value;
                
this .lblCurrectPage.Text  =  _mixPage.ToString();
            }
        }
     4. 显示页面分页信息
  ///   <summary>
        
///  显示页面分页信息
        
///   </summary>
         private   void  GetPageCount()
        {
            
if  (RecordCount  ==   0 )
            {
                MaxPage 
=   1 ;
            }
            
else
            {
                
if  ( this .RecordCount  %  PageSize  ==   0 )
                {
                    MaxPage 
=   this .RecordCount  /  PageSize;
                }
                
else
                {
                    MaxPage 
=   this .RecordCount  /  PageSize  +   1 ;
                }
            }
            
if  ( this .CurrentPage  ==  MixPage)
            {
                
this .First.Enabled  =   false ;
                
this .Pre.Enabled  =   false ;

                
if  (RecordCount  <=  PageSize)
                {
                    
this .Next.Enabled  =   false ;
                    
this .Last.Enabled  =   false ;
                }
                
else
                {
                    
this .Next.Enabled  =   true ;
                    
this .Last.Enabled  =   true ;
                }
            }
            
this .lblPageInfo.Text  =   string .Format(_lblPageInfo,  this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
            
this .lblMaxPage.Text  =   " / "   +  MaxPage;
            
this .lblCurrectPage.Text  =  CurrentPage.ToString();
        }
      5. 定义事件
         // 首页
         private   void  First_Click( object  sender, EventArgs e)
        {
            
this .CurrentPage  =  MixPage;
            
this .lblPageInfo.Text  =   string .Format(_lblPageInfo,  this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
            
this .lblMaxPage.Text  =   " / "   +  MaxPage;
            
this .lblCurrectPage.Text  =  CurrentPage.ToString();
            
this .First.Enabled  =   false ;
            
this .Pre.Enabled  =   false ;
            
this .Next.Enabled  =   true ;
            
this .Last.Enabled  =   true ;
            OnPropertyChanged(
" CurrentPage " );
        }

        
// 上一页
         private   void  Pre_Click( object  sender, EventArgs e)
        {
            
this .CurrentPage  =   this .CurrentPage  -   1 ;
            
this .lblPageInfo.Text  =   string .Format(_lblPageInfo,  this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
            
this .lblMaxPage.Text  =   " / "   +  MaxPage;
            
this .lblCurrectPage.Text  =  CurrentPage.ToString();
            
if  ( this .CurrentPage  ==   this .MixPage)
            {
                
this .First.Enabled  =   false ;
                
this .Pre.Enabled  =   false ;
                
this .Next.Enabled  =   true ;
                
this .Last.Enabled  =   true ;
            }
            
else
            {
                
this .First.Enabled  =   true ;
                
this .Pre.Enabled  =   true ;
                
this .Next.Enabled  =   true ;
                
this .Last.Enabled  =   true ;
            }
            OnPropertyChanged(
" CurrentPage " );
        }

        
// 下一页
         private   void  Next_Click( object  sender, EventArgs e)
        {
            
this .CurrentPage  =   this .CurrentPage  +   1 ;
            
this .lblPageInfo.Text  =   string .Format(_lblPageInfo,  this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
            
this .lblMaxPage.Text  =   " / "   +  MaxPage;
            
this .lblCurrectPage.Text  =  CurrentPage.ToString();
            
if  ( this .CurrentPage  ==  MaxPage)
            {
                
this .First.Enabled  =   true ;
                
this .Pre.Enabled  =   true ;
                
this .Next.Enabled  =   false ;
                
this .Last.Enabled  =   false ;
            }
            
else
            {
                
this .First.Enabled  =   true ;
                
this .Pre.Enabled  =   true ;
                
this .Next.Enabled  =   true ;
                
this .Last.Enabled  =   true ;
            }
            OnPropertyChanged(
" CurrentPage " );
        }

        
// 末页
         private   void  Last_Click( object  sender, EventArgs e)
        {
            
this .CurrentPage  =  MaxPage;
            
this .lblPageInfo.Text  =   string .Format(_lblPageInfo,  this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
            
this .lblMaxPage.Text  =   " / "   +  MaxPage;
            
this .lblCurrectPage.Text  =  CurrentPage.ToString();
            
this .First.Enabled  =   true ;
            
this .Pre.Enabled  =   true ;
            
this .Next.Enabled  =   false ;
            
this .Last.Enabled  =   false ;
            OnPropertyChanged(
" CurrentPage " );
        }


        
private   void  lblCurrectPage_TextChanged( object  sender, EventArgs e)
        {
            
if  ( ! String.IsNullOrEmpty(lblCurrectPage.Text))
            {
                
this .CurrentPage  =   int .Parse(lblCurrectPage.Text);
            }
            
else
            {
                
this .CurrentPage  =  MixPage;
            }
            
this .lblPageInfo.Text  =   string .Format(_lblPageInfo,  this .CurrentPage, MaxPage.ToString(), RecordCount.ToString(), PageSize.ToString());
            
this .lblMaxPage.Text  =   " / "   +  MaxPage;
            
this .lblCurrectPage.Text  =  CurrentPage.ToString();
            
if  ( this .CurrentPage  ==   this .MaxPage)
            {
                
this .First.Enabled  =   true ;
                
this .Pre.Enabled  =   true ;
                
this .Next.Enabled  =   false ;
                
this .Last.Enabled  =   false ;
            }
            
else   if  ( this .CurrentPage  ==   this .MixPage)
            {
                
this .First.Enabled  =   false ;
                
this .Pre.Enabled  =   false ;
                
this .Next.Enabled  =   true ;
                
this .Last.Enabled  =   true ;
            }
            
else
            {
                
this .First.Enabled  =   true ;
                
this .Pre.Enabled  =   true ;
                
this .Next.Enabled  =   true ;
                
this .Last.Enabled  =   true ;
            }
            
if  ( this .lblCurrectPage.Text  !=   "" )
            {
                
if  ( int .Parse( this .lblCurrectPage.Text)  >  MaxPage)
                {
                    MessageBox.Show(
" 不存在输入页码数据! " " 输入信息错误 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
this .lblCurrectPage.Text  =  MixPage.ToString();
                    
this .First.Enabled  =   false ;
                    
this .Pre.Enabled  =   false ;
                    
this .Next.Enabled  =   false ;
                    
this .Last.Enabled  =   false ;
                }
            }
            OnPropertyChanged(
" CurrentPage " );
        }

        
private   void  lblCurrectPage_KeyPress( object  sender, KeyPressEventArgs e)
        {
            
if  ((e.KeyChar  !=   8   &&   ! char .IsDigit(e.KeyChar))  &&  e.KeyChar  !=   13 )//文本框里只能输入数字
            {
                e.Handled 
=   true ;
            }
        }
控件基本完成。

三、调用方法
在列表页面的调用方法:
绑定列表信息:
  private   void  Bind()
 {
            DataList.DataBoxList(dataGridView1, winFromPager1.StartIndex, winFromPager1.EndIndex, strWhere.ToString());
 }
dataGridView1:绑定列表控件名称
winFromPager1.StartIndex:开始索引
winFromPager1.EndIndex:  结束索引
strWhere.ToString(): 查询条件

分页事件:
  private   void  winFromPager1_PropertyChanged( object  sender, PropertyChangedEventArgs e)
 {
            Bind(); //绑定列表
 }
效果图:

转载于:https://www.cnblogs.com/lc329857895/archive/2009/07/28/1533088.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值