码迷,mamicode.com
首页 > Windows程序 > 详细

winform窗体自定义控件

时间:2014-08-23 12:35:20      阅读:457      评论:0      收藏:0      [点我收藏+]

标签:winform   分页控件   style   blog   http   color   os   使用   io   

 

先上代码!!!

 

bubuko.com,布布扣
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Drawing;
  5 using System.Data;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 
 10 using System.Reflection;
 11 namespace ControlSet
 12 {
 13     //处理分页时的委托,内置各种方法
 14     public delegate void PagerHandler(SimplePager sender);
 15     public partial class SimplePager : UserControl
 16     {
 17         public SimplePager()
 18         {
 19             InitializeComponent();
 20         }
 21         
 22         protected override void OnLoad(EventArgs e)
 23         {
 24             //this.panel_Main.BringToFront();
 25             //让panel控件浮动在最下方
 26             this.panel_Main.SendToBack();
 27             //当前页的按钮风格
 28             this.btn_Number1.BackColor = this.Color;
 29             this.btn_Number1.ForeColor = Color.White;
 30             this.btn_Number1.Font = new Font("微软雅黑", 15, FontStyle.Bold);
 31             this.btn_Number1.Cursor = Cursors.Hand;
 32             this.btn_Number1.Location = new Point(206, 19);
 33 
 34             //上一页
 35             this.btn_privious.BackColor = Color.White;
 36             this.btn_privious.ForeColor = this.Color;
 37             this.btn_privious.Font = new Font("微软雅黑", 15, FontStyle.Bold);
 38             this.btn_privious.Cursor = Cursors.Hand;
 39 
 40 
 41             //下一页
 42             this.btn_Next.BackColor = Color.White;
 43             this.btn_Next.ForeColor = this.Color;
 44             this.btn_Next.Font = new Font("微软雅黑", 15, FontStyle.Bold);
 45             this.btn_Next.Cursor = Cursors.Hand;
 46 
 47             //尾页
 48             this.btn_last.BackColor = Color.White;
 49             this.btn_last.ForeColor = this.Color;
 50             this.btn_last.Font = new Font("微软雅黑", 15, FontStyle.Bold);
 51             this.btn_last.Cursor = Cursors.Hand;
 52             //其他页的设置和上一页下一页的风格一页
 53             base.OnLoad(e);
 54         }
 55 
 56         public void ClickSearch()
 57         {
 58             this.btn_last.Width = 33 + this.PageCount.ToString().Length * 10;
 59             this.btn_last.Text = this.PageCount.ToString();
 60             if (this.PageCount < 6)
 61             {
 62                 this.btn_Number1.BackColor = this.Color;
 63                 this.btn_Number1.ForeColor = Color.White;
 64                 this.btn_Number1.Font = new Font("微软雅黑", 15, FontStyle.Bold);
 65                 this.btn_Number1.Cursor = Cursors.Hand;
 66                 this.btn_Dot1.Visible = false;
 67                 this.btn_Dot2.Visible = false;
 68                 if (this.PageCount == 1)
 69                 {
 70                     this.btn_privious.Visible = false;
 71                     this.btn_Next.Visible = false;
 72                     this.btn_last.Visible = false;
 73                     this.panel_Main.Controls.Clear();
 74                     return;
 75                 }
 76                 this.btn_privious.Visible = false;
 77                 this.btn_Next.Visible = true;
 78 
 79                 this.panel_Main.Controls.Clear();
 80  
 81                 for (int i = 2; i <= PageCount; i++)
 82                 {
 83                     Button btn = new Button();
 84                     btn.Location = new Point(186 + (i - 1) * 42, 1);
 85                     btn.Name = "btn_Number" + i;
 86                     btn.Text = i.ToString();
 87                     btn.Size = new Size(41, 41);
 88                     btn.BackColor = Color.White;
 89                     btn.ForeColor = this.Color;
 90                     btn.Font = new Font("微软雅黑", 15, FontStyle.Bold);
 91                     btn.Cursor = Cursors.Hand;
 92                     this.panel_Main.Controls.Add(btn);
 93                     btn.Click += new EventHandler(btn_Click);
 94                 }
 95                 this.btn_privious.Location = new Point(this.btn_Number1.Location.X - 86, 20);
 96                 this.btn_Next.Location = new Point(290 + (this.panel_Main.Controls.Count - 1) * 42,20);
 97                 this.btn_last.Visible = false;
 98             }
 99             else if(this.PageCount == 6)
100             {
101                 this.IsSixPageCount();
102             }
103             else if(this.PageCount > 6)
104             {
105                 this.IsExceedSixPageCount();
106             }
107             
108         }
109         public void BindData()
110         {
111             this.btn_privious.Visible = (this.No != 1);
112             //this.btn_last.Visible = (this.No != this.PageCount);
113             this.btn_Next.Visible = (this.No != this.PageCount);
114             if(this.PageChanging != null)
115             {
116                 this.PageChanging(this);
117             }
118             
119         }
120 
121         public void ChangeColor()
122         {
123             if (this.No == 1)
124             {
125                 this.btn_Number1.BackColor = this.Color;
126                 this.btn_Number1.ForeColor = Color.White;
127                 this.btn_Number1.Font = new Font("微软雅黑", 15, FontStyle.Bold);
128                 this.btn_Number1.Cursor = Cursors.Hand;
129             }
130             else
131             {
132                 this.btn_Number1.BackColor = Color.White;
133                 this.btn_Number1.ForeColor = this.Color;
134                 this.btn_Number1.Font = new Font("微软雅黑", 15, FontStyle.Bold);
135                 this.btn_Number1.Cursor = Cursors.Hand;
136             }
137             if (this.No == PageCount)
138             {
139                 this.btn_last.BackColor = this.Color;
140                 this.btn_last.ForeColor = Color.White;
141                 this.btn_last.Font = new Font("微软雅黑", 15, FontStyle.Bold);
142                 this.btn_last.Cursor = Cursors.Hand;
143             }
144             else
145             {
146                 this.btn_last.BackColor = Color.White;
147                 this.btn_last.ForeColor = this.Color;
148                 this.btn_last.Font = new Font("微软雅黑", 15, FontStyle.Bold);
149                 this.btn_last.Cursor = Cursors.Hand;
150             }
151 
152             //上一页
153             this.btn_privious.BackColor = Color.White;
154             this.btn_privious.ForeColor = this.Color;
155             this.btn_privious.Font = new Font("微软雅黑", 15, FontStyle.Bold);
156             this.btn_privious.Cursor = Cursors.Hand;
157 
158 
159             //下一页
160             this.btn_Next.BackColor = Color.White;
161             this.btn_Next.ForeColor = this.Color;
162             this.btn_Next.Font = new Font("微软雅黑", 15, FontStyle.Bold);
163             this.btn_Next.Cursor = Cursors.Hand;
164 
165 
166             foreach (Control c in this.panel_Main.Controls)
167             {
168                 if (c.GetType() == typeof(Button) && c.Name.Contains("Number"))
169                 {
170                     int index = c.Name.LastIndexOf(r);
171                     if (c.Name.Substring(index + 1, c.Name.Length - index - 1) == this.No.ToString())
172                     {
173                         c.BackColor = this.Color;
174                         c.ForeColor = Color.White;
175                         c.Font = new Font("微软雅黑", 15, FontStyle.Bold);
176                         c.Cursor = Cursors.Hand;
177                     }
178                     else
179                     {
180                         c.BackColor = Color.White;
181                         c.ForeColor = this.Color;
182                         c.Font = new Font("微软雅黑", 15, FontStyle.Bold);
183                         c.Cursor = Cursors.Hand;
184                     }
185                 }
186             }
187         }
188 
189         //搜索页数小于6页时候,动态生成按钮数字的单机事件
190         void btn_Click(object sender, EventArgs e)
191         {
192             this.No = int.Parse(((Button)sender).Text);
193 
194             this.btn_privious.Visible = (this.No != 1);//上一页
195             //this.btn_last.Visible = (this.No != this.PageCount);
196             this.btn_Next.Visible = (this.No != this.PageCount);
197 
198             this.ChangeColor();
199 
200         }
201         public event PagerHandler PageChanging;
202 
203         /// <summary>
204         /// 当前是第几页
205         /// </summary>
206         public int No { get; set; }
207 
208         /// <summary>
209         /// 每页记录数,如每页10条记录
210         /// </summary>
211         /// <param name="sender"></param>
212         /// <param name="e"></param>
213 
214         public int PageSize { get; set; }
215 
216 
217         /// <summary>
218         /// 总记录数
219         /// </summary>
220         /// <param name="sender"></param>
221         /// <param name="e"></param>
222 
223         public int RecordCount { get; set; }
224 
225         /// <summary>
226         /// 总页数
227         /// </summary>
228         /// <param name="sender"></param>
229         /// <param name="e"></param>
230         public int PageCount 
231         {
232             get
233             {
234                 if (this.RecordCount == 0 || this.PageSize == 0)
235                 {
236                     return 0;
237                 }
238                 if (this.RecordCount % this.PageSize == 0)
239                 {
240                     return this.RecordCount / this.PageSize;
241                 }
242 
243                 return this.RecordCount / this.PageSize + 1;
244             }
245         }
246 
247         /// <summary>
248         /// 获取分页时候的开始索引
249         /// </summary>
250         /// <param name="sender"></param>
251         /// <param name="e"></param>
252 
253         public int StartIndex
254         {
255             get 
256             {
257                 return (this.No - 1) * this.PageSize + 1;
258             }
259         }
260 
261         /// <summary>
262         /// 获取结束索引
263         /// </summary>
264         /// <param name="sender"></param>
265         /// <param name="e"></param>
266         public int EndIndex
267         {
268             get 
269             {
270                 return this.No * this.PageSize;
271             }
272         }
273         private Color color = Color.FromArgb(0, 153, 96);
274 
275         public Color Color
276         {
277             get { return color; }
278             set { color = value; }
279         }
280         private void SimplePager_Load(object sender, EventArgs e)
281         {
282 
283         }
284 
285         private void btn_privious_Click(object sender, EventArgs e)
286         {
287             this.No--;
288             this.IsSixPageCount();
289             this.IsExceedSixPageCount();
290             this.ChangeColor();
291             this.BindData();
292         }
293 
294         private void IsSixPageCount()
295         {
296             if (this.PageCount == 6)
297             {
298                 if (this.No == 1)
299                 {
300                     this.btn_Number1.BackColor = this.Color;
301                     this.btn_Number1.ForeColor = Color.White;
302                     this.btn_Number1.Font = new Font("微软雅黑", 15, FontStyle.Bold);
303                     this.btn_Number1.Cursor = Cursors.Hand;
304                     this.btn_Number1.Location = new Point(206, 19);
305 
306                     this.btn_Dot1.Visible = false;
307                     this.btn_Dot2.Visible = true;
308                     this.btn_Next.Visible = true;
309 
310                     this.panel_Main.Controls.Clear();
311                     for (int i = 2; i < 6; i++)
312                     {
313                         Button btn6 = new Button();
314                         btn6.Location = new Point(186 + (i - 1) * 42, 1);
315                         btn6.Name = "btn_Number" + i;
316                         btn6.Text = i.ToString();
317                         btn6.Size = new Size(41, 41);
318                         btn6.BackColor = Color.White;
319                         btn6.ForeColor = this.Color;
320                         btn6.Font = new Font("微软雅黑", 15, FontStyle.Bold);
321                         btn6.Cursor = Cursors.Hand;
322                         this.panel_Main.Controls.Add(btn6);
323                         btn6.Click += (sendar, args) =>
324                         {
325                             this.No = int.Parse(((Button)sendar).Text);
326                             this.IsSixPageCount();
327                             this.btn_privious.Visible = (this.No != 1);//上一页
328                             //this.btn_last.Visible = (this.No != this.PageCount);
329                             this.btn_Next.Visible = (this.No != this.PageCount);
330                             this.ChangeColor();
331                         };
332                     }
333                     this.btn_Dot2.Location = new Point(290 + (this.panel_Main.Controls.Count - 1) * 42, 19);
334                     this.btn_privious.Location = new Point(this.btn_Number1.Location.X - 86, 20);
335                     this.btn_Next.Location = new Point(290 + (this.panel_Main.Controls.Count) * 42, 20);
336                     this.btn_last.Visible = false;
337                 }
338             }
339             if (this.PageCount == 6)
340             {
341                 this.btn_Dot1.Visible = false;
342                 if (!this.panel_Main.Controls.Contains(this.panel_Main.Controls["btn_Number6"]))
343                 {
344                     this.btn_Dot2.Visible = false;
345                     Button btn = new Button();
346                     btn.Location = new Point(186 + (6 - 1) * 42, 1);
347                     btn.Name = "btn_Number" + 6;
348                     btn.Text = "6";
349                     btn.Size = new Size(41, 41);
350                     btn.BackColor = Color.White;
351                     btn.ForeColor = this.Color;
352                     btn.Font = new Font("微软雅黑", 15, FontStyle.Bold);
353                     btn.Cursor = Cursors.Hand;
354                     this.panel_Main.Controls.Add(btn);
355                     this.btn_Next.Location = new Point(290 + (this.panel_Main.Controls.Count - 1) * 42, 20);
356                     btn.Click += (obj, args) =>
357                     {
358                         Button btns = (Button)obj;
359                         this.No = 6;
360                         this.btn_Next.Visible = false;
361                         this.ChangeColor();
362                     };
363                 }
364                 if (this.PageCount - this.No > 2)
365                 {
366                     Button c = (Button)this.panel_Main.Controls["btn_Number6"];
367                     c.Visible = false;
368                     this.btn_Dot2.Visible = true;
369                 }
370                 else
371                 {
372                     Button c = (Button)this.panel_Main.Controls["btn_Number6"];
373                     c.Visible = true;
374                     this.btn_Dot2.Visible = false;
375                 }
376             }
377         }
378         private void PageStyleOne()
379         {
380             this.btn_Number1.Location = new Point(206, 19);
381             this.panel_Main.Controls.Clear();
382             for (int i = 2; i < 6; i++)
383             {
384                 Button btnExceed6 = new Button();
385                 btnExceed6.Location = new Point(186 + (i - 1) * 42, 1);
386                 btnExceed6.Name = "btn_Number" + i;
387                 btnExceed6.Text = i.ToString();
388                 btnExceed6.Size = new Size(41, 41);
389                 btnExceed6.BackColor = Color.White;
390                 btnExceed6.ForeColor = this.Color;
391                 btnExceed6.Font = new Font("微软雅黑", 15, FontStyle.Bold);
392                 btnExceed6.Cursor = Cursors.Hand;
393                 this.panel_Main.Controls.Add(btnExceed6);
394                 btnExceed6.Click += (sendar, args) =>
395                 {
396                     this.No = int.Parse(((Button)sendar).Text);
397                     this.IsExceedSixPageCount();
398                 };
399             }
400             this.btn_Dot1.Visible = false;
401             this.btn_Dot2.Visible = true;
402             this.btn_Next.Visible = true;
403             this.btn_Dot2.Location = new Point(290 + (this.panel_Main.Controls.Count - 1) * 42, 19);
404             this.btn_privious.Location = new Point(this.btn_Number1.Location.X - 86, 20);
405             this.btn_last.Visible = true;
406             this.btn_last.Location = new Point(290 + (this.panel_Main.Controls.Count) * 42, 20);
407             this.btn_last.BackColor = Color.White;
408             this.btn_last.ForeColor = this.Color;
409             this.btn_last.Font = new Font("微软雅黑", 15, FontStyle.Bold);
410             this.btn_last.Cursor = Cursors.Hand;
411 
412             this.btn_Next.Location = new Point(this.btn_last.Location.X + this.btn_last.Width, 20);
413             this.ChangeColor();
414         }
415         private void PageStyleSpecial()
416         {
417             this.panel_Main.Controls.Clear();
418             this.btn_Dot1.Visible = false;
419             this.btn_Dot2.Visible = false;
420 
421             for (int i = this.No - 2; i < this.PageCount; i++)
422             {
423                 Button btning = new Button();
424                 btning.Location = new Point(186 + (i - 1) * 42, 1);
425                 btning.Name = "btn_Number" + i;
426                 btning.Text = i.ToString();
427                 btning.Size = new Size(41, 41);
428                 btning.BackColor = Color.White;
429                 btning.ForeColor = this.Color;
430                 btning.Font = new Font("微软雅黑", 15, FontStyle.Bold);
431                 btning.Cursor = Cursors.Hand;
432                 this.panel_Main.Controls.Add(btning);
433                 //对动态生成的按钮写单击事件
434                 btning.Click += (sendar, e) =>
435                 {
436                     this.No = int.Parse(((Button)sendar).Text);
437                     this.btn_privious.Visible = (this.No != 1);//上一页
438                     //this.btn_last.Visible = (this.No != this.PageCount);
439                     this.btn_Next.Visible = (this.No != this.PageCount);
440 
441                     this.ChangeColor();
442                     this.IsExceedSixPageCount();
443                 };
444             }
445             //this.btn_Dot2.Location = new Point(290 + (this.panel_Main.Controls.Count - 1) * 42, 19);
446             this.btn_privious.Location = new Point(this.btn_Number1.Location.X - 86, 20);
447             this.btn_last.Visible = true;
448             this.btn_last.Location = new Point(290 + (this.panel_Main.Controls.Count - 1) * 42, 20);
449             this.btn_Next.Location = new Point(this.btn_last.Location.X + this.btn_last.Width, 20);
450             this.ChangeColor();
451         }
452         //当页数超过6页时候应该调用的方法
453         private void IsExceedSixPageCount()
454         {
455             if(this.PageCount > 6)
456             {
457                 if (this.No == 1)
458                 {
459                     this.PageStyleOne();
460                 }
461                 //点击的是最后一页,显示最后5页 无下一页
462                 else if (this.No == this.PageCount)
463                 {
464                     PageStyleLast();
465                 }
466                 //非常特殊的情况,刚好分7页,而正好当前页为最中间的第4页
467                 else if (this.No == 4)
468                 {
469                     if (this.PageCount == 7)
470                     {
471                         this.PageStyleSpecial();
472                     }
473                     //表示点击第4页 页数大于7和点击1时的风格一样
474                     else
475                     {
476                         this.PageStyleOne();
477                     }
478                 }
479                 else if (this.No < 4)//这种情况和this.No == 1的显示方式差不多
480                 {
481                     this.PageStyleOne();
482                 }
483                 //需要使用点1显示省略部分
484                 else if (this.No > 4)
485                 {
486                     this.btn_Dot1.Visible = true;
487                     this.btn_Dot1.Location = new Point(this.btn_Number1.Location.X + 42, 19);
488                     //有第一个省略号,无第二个省略号显示风格为1... 7 8 9 10 11 下一页
489                     if (this.PageCount - this.No < 4)
490                     {
491                         PageStyleDotOne();
492                     }
493                     //第一个和第二个省略号都有,上一页1...6,7,8,9,10...24下一页
494                     else if (this.PageCount - this.No >= 4)
495                     {
496                         PageStyleDotOneDotTwo();
497                     }
498                 }
499                 
500             }
501         }
502 
503         //还需要继续写代码优化性能
504         private void PageStyleLast()
505         {
506             this.panel_Main.Controls.Clear();
507             this.btn_Dot1.Visible = true;
508             this.btn_Dot1.Location = new Point(this.btn_Number1.Location.X + 42, 19);
509             this.btn_Dot2.Visible = false;
510             int j = 0;
511             //需要动态计算
512             for (int i = this.PageCount - 4; i < this.PageCount; i++)
513             {
514                 Button btning = new Button();
515                 btning.Location = new Point(this.btn_Dot1.Location.X + j * (33 + this.PageCount.ToString().Length * 10) + 21, 1);
516                 btning.Name = "btn_Number" + i;
517                 btning.Text = i.ToString();
518                 btning.Size = new Size(41, 41);
519                 btning.BackColor = Color.White;
520                 btning.ForeColor = this.Color;
521                 btning.Font = new Font("微软雅黑", 15, FontStyle.Bold);
522                 btning.Cursor = Cursors.Hand;
523                 btning.Width = 33 + this.PageCount.ToString().Length * 10;
524                 this.panel_Main.Controls.Add(btning);
525                 //对动态生成的按钮写单击事件
526                 btning.Click += (sendar, e) =>
527                 {
528                     this.No = int.Parse(((Button)sendar).Text);
529                     this.btn_privious.Visible = (this.No != 1);//上一页
530                     //this.btn_last.Visible = (this.No != this.PageCount);
531                     this.btn_Next.Visible = (this.No != this.PageCount);
532                     this.IsExceedSixPageCount();
533                 };
534                 j++;
535             }
536             this.btn_Next.Visible = false;
537             this.btn_last.Visible = true;
538             this.btn_last.Location = new Point(290 + (this.panel_Main.Controls.Count) * (33 + this.PageCount.ToString().Length * 10), 20);
539             this.ChangeColor();
540         }
541 
542         private void PageStyleDotOneDotTwo()
543         {
544             if (this.panel_Main.Controls.Count != 5)
545             {
546                 this.panel_Main.Controls.Clear();
547                 int j = 0;
548                 for (int i = this.No - 2; i <= this.No + 2; i++)
549                 {
550                     Button btning = new Button();
551                     //由于页数过多,需要动态编写按钮的位置
552                     btning.Location = new Point(this.btn_Dot1.Location.X + j * (33 + (this.No + 2).ToString().Length * 10) + 21, 1);
553                     btning.Name = "btn_Number" + i;
554                     btning.Text = i.ToString();
555                     btning.Size = new Size(41, 41);
556                     btning.BackColor = Color.White;
557                     btning.ForeColor = this.Color;
558                     btning.Font = new Font("微软雅黑", 15, FontStyle.Bold);
559                     btning.Cursor = Cursors.Hand;
560                     btning.Width = 33 + (this.No + 2).ToString().Length * 10;
561                     this.panel_Main.Controls.Add(btning);
562                     //对动态生成的按钮写单击事件
563                     btning.Click += (sendar, e) =>
564                     {
565                         this.No = int.Parse(((Button)sendar).Text);
566                         this.btn_privious.Visible = (this.No != 1);//上一页
567                         //this.btn_last.Visible = (this.No != this.PageCount);
568                         this.btn_Next.Visible = (this.No != this.PageCount);
569 
570                         this.ChangeColor();
571                         this.IsExceedSixPageCount();
572                     };
573                     j++;
574                 }
575                 this.btn_Dot2.Visible = true;
576                 this.btn_Dot2.Location = new Point(290 + (this.panel_Main.Controls.Count) * (33 + (this.No + 2).ToString().Length * 10), 20);
577                 this.btn_Dot2.Visible = true;
578                 this.btn_privious.Location = new Point(this.btn_Number1.Location.X - 86, 20);
579                 this.btn_last.Visible = true;
580                 this.btn_last.Location = new Point(this.btn_Dot2.Location.X + this.btn_Dot2.Width, 20);
581                 this.btn_Next.Location = new Point(this.btn_last.Location.X + (33 + (this.PageCount.ToString().Length) * 10), 20);
582                 this.ChangeColor();
583             }
584             else if(this.panel_Main.Controls.Count == 5)
585             {
586                 int j = 0;
587                 int i = this.No - 2;
588                 foreach(Control c in this.panel_Main.Controls)
589                 {
590                     Button buttoning = c as Button;
591                     buttoning.Name = "btn_Number" + i;
592                     buttoning.Text = i.ToString();
593                     if((this.No + 2)/10 - (this.No - 2)/10 ==1 || (this.No + 2)/10 - (this.No - 2)/10 == 0)
594                     {
595                         buttoning.Location = new Point(this.btn_Dot1.Location.X + j * (33 + (this.No + 2).ToString().Length * 10) + 21, 1);
596                         buttoning.Width = 33 + (this.No + 2).ToString().Length * 10;
597                     }
598 
599                     this.ClearEvent<Button>(buttoning);
600 
601                     buttoning.Click += (sendar, e) =>
602                     {
603                         this.No = int.Parse(((Button)sendar).Text);
604                         this.btn_privious.Visible = (this.No != 1);//上一页
605                         //this.btn_last.Visible = (this.No != this.PageCount);
606                         this.btn_Next.Visible = (this.No != this.PageCount);
607                         this.IsExceedSixPageCount();
608                     };
609                     i++;
610                     j++;
611                 }
612 
613                 this.btn_Dot2.Visible = true;
614                 this.btn_Dot2.Location = new Point(290 + (this.panel_Main.Controls.Count) * (33 + (this.No + 2).ToString().Length * 10), 20);
615                 this.btn_Dot2.Visible = true;
616                 this.btn_privious.Location = new Point(this.btn_Number1.Location.X - 86, 20);
617                 this.btn_last.Visible = true;
618                 this.btn_last.Location = new Point(this.btn_Dot2.Location.X + this.btn_Dot2.Width, 20);
619                 this.btn_Next.Location = new Point(this.btn_last.Location.X + (33 + (this.PageCount.ToString().Length) * 10), 20);
620                 this.ChangeColor();
621             }
622         }
623 
624         private void PageStyleDotOne()
625         {
626             this.btn_Dot2.Visible = false;
627             int j = 0;
628             if (this.panel_Main.Controls.Count != 4)
629             {
630                 j = 0;
631                 this.panel_Main.Controls.Clear();
632                 for (int i = this.PageCount - 4; i < this.PageCount; i++)
633                 {
634                     Button btning = new Button();
635                     btning.Location = new Point(this.btn_Dot1.Location.X + j * (33 + this.PageCount.ToString().Length * 10) + 21, 1);
636                     btning.Name = "btn_Number" + i;
637                     btning.Text = i.ToString();
638                     btning.Size = new Size(41, 41);
639                     btning.BackColor = Color.White;
640                     btning.ForeColor = this.Color;
641                     btning.Font = new Font("微软雅黑", 15, FontStyle.Bold);
642                     btning.Cursor = Cursors.Hand;
643                     btning.Width = 33 + this.PageCount.ToString().Length * 10;
644                     this.panel_Main.Controls.Add(btning);
645                     //对动态生成的按钮写单击事件
646                     btning.Click += (sendar, e) =>
647                     {
648                         this.No = int.Parse(((Button)sendar).Text);
649                         this.btn_privious.Visible = (this.No != 1);//上一页
650                         //this.btn_last.Visible = (this.No != this.PageCount);
651                         this.btn_Next.Visible = (this.No != this.PageCount);
652                         this.IsExceedSixPageCount();
653                     };
654                     j++;
655                 }
656                 this.btn_last.Visible = true;
657                 this.btn_last.Location = new Point(290 + (this.panel_Main.Controls.Count) * (33 + this.PageCount.ToString().Length * 10), 20);
658                 this.btn_Next.Visible = true;
659                 this.btn_Next.Location = new Point(this.btn_last.Location.X + (33 + this.PageCount.ToString().Length * 10), 20);
660                 this.ChangeColor();
661             }
662             else
663             {
664                 int i = this.PageCount - 4;
665                 j = 0;
666                 foreach(Control c in this.panel_Main.Controls)
667                 {
668                     Button btning = c as Button;
669                     btning.Name = "btn_Number" + i;
670                     btning.Text = i.ToString();
671                     this.ClearEvent<Button>(btning);
672                     btning.Click += (sendar, args) =>
673                     {
674                         this.No = int.Parse(((Button)sendar).Text);
675                         this.btn_privious.Visible = (this.No != 1);//上一页
676                         //this.btn_last.Visible = (this.No != this.PageCount);
677                         this.btn_Next.Visible = (this.No != this.PageCount);
678                         this.IsExceedSixPageCount();
679                     };
680                     i++;
681                     j++;
682                 }
683                 this.btn_last.Visible = true;
684                 this.btn_last.Location = new Point(290 + (this.panel_Main.Controls.Count) * (33 + this.PageCount.ToString().Length * 10), 20);
685                 this.btn_Next.Visible = true;
686                 this.btn_Next.Location = new Point(this.btn_last.Location.X + (33 + this.PageCount.ToString().Length * 10), 20);
687                 this.ChangeColor();
688             }
689         }
690 
691         private void btn_Next_Click(object sender, EventArgs e)
692         {
693             this.No++;
694             this.IsSixPageCount();
695             this.IsExceedSixPageCount();
696             this.ChangeColor();
697             this.BindData();
698         }
699 
700         private void btn_display_Click(object sender, EventArgs e)
701         {
702             //第一页
703             this.No = 1;
704             this.BindData();
705             this.IsSixPageCount();
706 
707             this.IsExceedSixPageCount();
708             this.ChangeColor();
709         }
710 
711         private void btn_last_Click(object sender, EventArgs e)
712         {
713             this.No = this.PageCount;
714             this.BindData();
715             this.IsSixPageCount();
716             this.IsExceedSixPageCount();
717             this.ChangeColor();
718         }
719         #region 传入控件就对控件的所有事件委托清空的方法
720         public void ClearEvent<T>(Control Ctl)
721         {
722             Delegate[] invokeList = GetObjectEvenList(Ctl, "EventClick");
723             if (invokeList != null)
724             {
725                 foreach (Delegate del in invokeList)
726                 {
727                     typeof(T).GetEvent("Click").RemoveEventHandler(Ctl, del);
728                 }
729             }
730         }
731         /// <summary>
732         /// 获取控件事件
733         /// </summary>
734         /// <param name="p_Control">对象</param>
735         /// <param name="p_EventName">事件名</param>
736         /// <returns>委托列</returns>
737         public Delegate[] GetObjectEvenList(Control p_Control, string p_EventName)
738         {
739             PropertyInfo PropertyInfo = p_Control.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
740             if (PropertyInfo != null)
741             {
742                 object EventList = PropertyInfo.GetValue(p_Control, null);
743                 if (EventList != null && EventList is EventHandlerList)
744                 {
745                     EventHandlerList List = (EventHandlerList)EventList;
746                     FieldInfo FieldInfo = (typeof(Control)).GetField(p_EventName, BindingFlags.Static | BindingFlags.NonPublic);
747                     if (FieldInfo == null)
748                     {
749                         return null;
750                     }
751                     Delegate ObjectDeletegate = List[FieldInfo.GetValue(p_Control)];
752                     if (ObjectDeletegate == null)
753                     {
754                         return null;
755                     }
756 
757                     return ObjectDeletegate.GetInvocationList();
758                 }
759             }
760             return null;
761         }
762         #endregion
763     }
764 }
View Code                      
学完委托,事件后的练手分页控件,有不足之处请指出。分页上10万条数据后会有延迟,不知道用多线程处理会不会更好,本人小菜鸟一个

winform窗体自定义控件

标签:winform   分页控件   style   blog   http   color   os   使用   io   

原文地址:http://www.cnblogs.com/suweiyuan/p/3930860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!