【NET】Winform分页控件初探

  1 public partial class WinFormPager : UserControl
  2     {
  3         const int PAGE_INDEX_START = 1;
  4         /// <summary>
  5         /// 当前页 
  6         /// </summary>
  7         public int CurrentPage
  8         {
  9             get;
 10             set;
 11         }
 12         /// <summary>
 13         /// 每页显示条数
 14         /// </summary>
 15         [Description("每页显示条数"), Category("分页设置"), DefaultValue(10)]
 16         public int PageSize
 17         {
 18             get;
 19             set;
 20         }
 21         /// <summary>
 22         /// 总共多少页 
 23         /// </summary>
 24         public int PageTotal
 25         {
 26             get;
 27             set;
 28         }
 29         int recordCount;
 30         public int RecordCount
 31         {
 32             get {
 33                 return recordCount;
 34             }
 35             set {
 36                 recordCount = value;
 37                 SetBtnPrePageAndBtnNextPage();
 38             }
 39         }
 40 
 41         private Label lbMsg;
 42         private LinkLabel llFirst;
 43         private LinkLabel llPrev;
 44         private LinkLabel llNext;
 45         private LinkLabel llLast;//按钮间距离
 46 
 47         public delegate void PageChangeDelegate();
 48         /// <summary>
 49         /// 当前页改变时发生的事件
 50         /// </summary>
 51         [Description("当前页改变时发生的事件"), Category("分页设置")]
 52         public event PageChangeDelegate PageChanged;
 53         public WinFormPager()
 54         {
 55             InitializeComponent();
 56         }
 57         
 58         /// <summary>
 59         /// 设置上一页、下一页是否可用以及当前页按钮字体颜色
 60         /// </summary>
 61         public void SetBtnPrePageAndBtnNextPage()
 62         {
 63             
 64             if (recordCount == 0)
 65             {
 66                 llFirst.Enabled = llPrev.Enabled = llNext.Enabled = llLast.Enabled = false;
 67             }
 68             else
 69             {
 70                 PageTotal = Convert.ToInt32(Math.Ceiling(RecordCount * 1.0 / PageSize));//总共多少页  
 71                 if (CurrentPage == PAGE_INDEX_START)
 72                 {
 73                     llFirst.Enabled = llPrev.Enabled = false;
 74                 }
 75                 else
 76                 {
 77                     llFirst.Enabled = llPrev.Enabled = true;
 78                 }
 79 
 80                 if (CurrentPage == PageTotal)
 81                 {
 82                     llNext.Enabled = llLast.Enabled = false;
 83                 }
 84                 else
 85                 {
 86                     llNext.Enabled = llLast.Enabled = true;
 87                 }
 88             }
 89             this.lbMsg.Text = string.Format(this.lbMsg.Tag.ToString(), RecordCount, PageSize, CurrentPage, PageTotal);
 90         }
 91 
 92 
 93         private void InitializeComponent()
 94         {
 95             this.lbMsg = new System.Windows.Forms.Label();
 96             this.llFirst = new System.Windows.Forms.LinkLabel();
 97             this.llPrev = new System.Windows.Forms.LinkLabel();
 98             this.llNext = new System.Windows.Forms.LinkLabel();
 99             this.llLast = new System.Windows.Forms.LinkLabel();
100             this.SuspendLayout();
101             // 
102             // lbMsg
103             // 
104             this.lbMsg.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
105             this.lbMsg.AutoSize = true;
106             this.lbMsg.Location = new System.Drawing.Point(12, 9);
107             this.lbMsg.Name = "lbMsg";
108             this.lbMsg.Size = new System.Drawing.Size(215, 19);
109             this.lbMsg.TabIndex = 0;
110             this.lbMsg.Tag = "共{0}条 {1}条/页 当前第{2}页/{3}页";
111             this.lbMsg.Text = "共{0}条 {1}条/页 当前第{2}页/{3}页";
112             this.lbMsg.UseCompatibleTextRendering = true;
113             // 
114             // llFirst
115             // 
116             this.llFirst.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
117             this.llFirst.AutoSize = true;
118             this.llFirst.Location = new System.Drawing.Point(279, 9);
119             this.llFirst.Name = "llFirst";
120             this.llFirst.Size = new System.Drawing.Size(29, 12);
121             this.llFirst.TabIndex = 1;
122             this.llFirst.TabStop = true;
123             this.llFirst.Text = "首页";
124             this.llFirst.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.llFirst_LinkClicked);
125             // 
126             // llPrev
127             // 
128             this.llPrev.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
129             this.llPrev.AutoSize = true;
130             this.llPrev.Location = new System.Drawing.Point(314, 9);
131             this.llPrev.Name = "llPrev";
132             this.llPrev.Size = new System.Drawing.Size(41, 12);
133             this.llPrev.TabIndex = 2;
134             this.llPrev.TabStop = true;
135             this.llPrev.Text = "上一页";
136             this.llPrev.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.llPrev_LinkClicked);
137             // 
138             // llNext
139             // 
140             this.llNext.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
141             this.llNext.AutoSize = true;
142             this.llNext.Location = new System.Drawing.Point(361, 9);
143             this.llNext.Name = "llNext";
144             this.llNext.Size = new System.Drawing.Size(41, 12);
145             this.llNext.TabIndex = 3;
146             this.llNext.TabStop = true;
147             this.llNext.Text = "下一页";
148             this.llNext.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.llNext_LinkClicked);
149             // 
150             // llLast
151             // 
152             this.llLast.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
153             this.llLast.AutoSize = true;
154             this.llLast.Location = new System.Drawing.Point(408, 9);
155             this.llLast.Name = "llLast";
156             this.llLast.Size = new System.Drawing.Size(29, 12);
157             this.llLast.TabIndex = 4;
158             this.llLast.TabStop = true;
159             this.llLast.Text = "末页";
160             this.llLast.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.llLast_LinkClicked);
161             // 
162             // WinFormPager
163             // 
164             this.Controls.Add(this.llLast);
165             this.Controls.Add(this.llNext);
166             this.Controls.Add(this.llPrev);
167             this.Controls.Add(this.llFirst);
168             this.Controls.Add(this.lbMsg);
169             this.Name = "WinFormPager";
170             this.Size = new System.Drawing.Size(468, 30);
171             this.ResumeLayout(false);
172             this.PerformLayout();
173 
174         }
175 
176         private void llFirst_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
177         {
178             PageAction(PAGE_INDEX_START);
179             SetBtnPrePageAndBtnNextPage();
180         }
181 
182         public void PageAction(int pageNumOperation) {
183             CurrentPage = pageNumOperation;
184             if (CurrentPage < PAGE_INDEX_START)
185             {
186                 CurrentPage = PAGE_INDEX_START;
187             }
188             if (CurrentPage > PageTotal) {
189                 CurrentPage = PageTotal;
190             }
191             
192             if (PageChanged != null)
193             {
194                 PageChanged();
195             }
196         }
197 
198         private void llLast_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
199         {
200             PageAction(PageTotal);
201         }
202 
203         private void llPrev_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
204         {
205             PageAction(CurrentPage-1);
206         }
207 
208         private void llNext_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
209         {
210             PageAction(CurrentPage + 1);
211         }
212 
213     }

调用

public ListViewExtendPager()
        {//分页List 控件(此控件包含分页控件)构造函数
            InitializeComponent();
            this.wfpPager.PageSize = 15;
            this.wfpPager.PageChanged += new WinFormPager.PageChangeDelegate(
                () =>
                {
                    DataBind();
                }
            );
        }
        public Action<ListViewExtendPager> dataBindAction;
        private void ListViewExtendPager_Load(object sender, EventArgs e)
        {
            this.wfpPager.CurrentPage = 1;
            DataBind();
        }

        private void DataBind() {
            if (dataBindAction != null) {
                dataBindAction(this);
            }
        }

 

posted @ 2013-07-26 10:22  哈哈好玩  阅读(343)  评论(0编辑  收藏  举报