C# winfrom 中自定义的翻页控件(自己设计)

2021-08-01 18:55

阅读:922

标签:val   点击   view   text   minimum   class   页面   dem   global   1.主要是使用控件绑定点击事件    用到的控件分别为picturebox   lable  上一页pbPage_Prev    下一页 pbPage_Next  首页 pbPage_Begin   尾页pbPage_End  是picturebox控件加背景图   “第  页/ 共  页” 是一个lable “labPageInfo”    在lable上面加了一个隐藏的textbox 控件 “txtPageInfo” 2.将这个翻页的功能单独写在用户控件 ucPageTurn 里面  然后在每个页面直接应用就可以了   下面只是把ucPageTurn写了出来 还需要在winform页面上应用上 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 namespace Demo 11 { 12 public partial class ucPageTurn : UserControl, IMessageFilter 13 { 14 private const int SIZE_HEIGHT = 40; 15 private const int SIZE_MINWIDTH = 84; 16 private const int SIZE_INFO_MINWIDTH = 188; 17 18 public ucPageTurn() 19 { 20 Application.AddMessageFilter(this); 21 22 InitializeComponent(); 23 this.BorderStyle = System.Windows.Forms.BorderStyle.None; 24 this.MinimumSize = new Size(SIZE_MINWIDTH, 0); 25 this.Disposed += new EventHandler(ucPages_Disposed); 26 27 //this.MouseClick += new MouseEventHandler(ucKeyboard_Close); 28 29 PageChanged += new PageChangedHandle(new PageChangedHandle((oldPage, newPage, e) => { })); 30 InputGotFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { })); 31 //InputLostFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { })); 32 33 InputGotFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { })); 34 InputLostFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { })); 35 36 InputMouseDown += new InputMouseHandle(new InputMouseHandle((sender, e) => { })); 37 InputMouseUp += new InputMouseHandle(new InputMouseHandle((sender, e) => { })); 38 InputTextClick += new EventHandler(new EventHandler((sender, e) => { })); 39 InputKeyDown += new InputKeyHandle(new InputKeyHandle((sender, e) => { })); 40 InputKeyUp += new InputKeyHandle(new InputKeyHandle((sender, e) => { })); 41 InputKeyPress += new InputKeyPressHandle(new InputKeyPressHandle((sender, e) => { })); 42 InputTextChanged += new EventHandler(new EventHandler((sender, e) => { })); 43 44 this.BackColor = Color.White; 45 labPageInfo.BackColor = this.BackColor; 46 47 this.Resize += new EventHandler(ucPages_Resize); 48 49 //labPageInfo.MouseDoubleClick += new MouseEventHandler(labPageInfo_MouseDoubleClick); 50 51 pbPage_Prev.MouseDown += new MouseEventHandler(pbPage_Prev_MouseDown); 52 pbPage_Prev.MouseUp += new MouseEventHandler(pbPage_Prev_MouseUp); 53 54 pbPage_Next.MouseDown += new MouseEventHandler(pbPage_Next_MouseDown); 55 pbPage_Next.MouseUp += new MouseEventHandler(pbPage_Next_MouseUp); 56 57 pbPage_Begin.MouseDown += new MouseEventHandler(pbPage_Begin_MouseDown); 58 pbPage_Begin.MouseUp += new MouseEventHandler(pbPage_Begin_MouseUp); 59 60 pbPage_End.MouseDown += new MouseEventHandler(pbPage_End_MouseDown); 61 pbPage_End.MouseUp += new MouseEventHandler(pbPage_End_MouseUp); 62 63 txtPageInfo.TextChanged += new EventHandler(txtPageInfo_TextChanged); 64 txtPageInfo.GotFocus += new EventHandler(txtPageInfo_GotFocus); 65 txtPageInfo.Click += new EventHandler(txtPageInfo_Click); 66 txtPageInfo.Text = m_strText; 67 txtPageInfo.Visible = m_blnShowTxtPageInfo; 68 69 m_blnIsAutoJump = false; 70 m_timerAutoPage.Enabled = false; 71 m_timerAutoPage.Interval = WAIT_FOR_AUTOJUMP; 72 m_timerAutoPage.Tick += new EventHandler(timerAutoPage_Tick); 73 } 74 75 private void ucPages_Load(object sender, EventArgs e) 76 { 77 setStatus(); 78 } 79 80 private void ucPages_Disposed(object sender, EventArgs e) 81 { 82 Application.RemoveMessageFilter(this); 83 } 84 85 public bool PreFilterMessage(ref System.Windows.Forms.Message MyMessage) 86 { 87 if (MyMessage.Msg == 0x204 || MyMessage.Msg == 0x205) 88 { 89 return true; 90 } 91 return false; 92 } 93 94 //设置控件的自适应大小 95 private void ucPages_Resize(object sender, EventArgs e) 96 { 97 this.Height = SIZE_HEIGHT; 98 99 pbPage_Begin.Location = new Point(0, 0); 100 pbPage_Begin.Size = new Size(SIZE_HEIGHT, SIZE_HEIGHT); 101 102 pbPage_Prev.Location = new Point(pbPage_Begin.Width + 2, pbPage_Begin.Top); 103 pbPage_Prev.Size = pbPage_Begin.Size; 104 105 pbPage_End.Location = new Point(this.Width - pbPage_End.Width, pbPage_Begin.Top); 106 pbPage_End.Size = pbPage_Begin.Size; 107 108 pbPage_Next.Location = new Point(this.Width - pbPage_Next.Width - pbPage_End.Width - 2, pbPage_Begin.Top); 109 pbPage_Next.Size = pbPage_Begin.Size; 110 111 if (this.Width = m_intPageCount) || (m_blnIsPrevDown && m_intCurPageIndex =m_intPageCount) || (m_blnIsBeginDown && m_intCurPageIndex= m_intPageCount) || (m_blnIsPrevDown && m_intCurPageIndex = m_intPageCount) || (m_blnIsBeginDown && m_intCurPageIndex 0 && value > m_intPageCount)) return; 390 391 int intOldPage = m_intCurPageIndex; 392 m_intCurPageIndex = value; 393 394 setStatus(); 395 if (!m_blnIgnoreChange) PageChanged(intOldPage, m_intCurPageIndex, new EventArgs()); 396 397 m_blnIsAutoJump = false; 398 m_timerAutoPage.Stop(); 399 m_timerAutoPage.Enabled = false; 400 } 401 } 402 403 //计算总页数 404 public int PageCount 405 { 406 get { return m_intPageCount; } 407 set 408 { 409 m_intPageCount = value; 410 411 if (m_intPageCount > 0) 412 { 413 if (m_intCurPageIndex m_intPageCount) 416 { 417 m_intCurPageIndex = m_intPageCount; 418 } 419 } 420 else 421 { 422 m_intCurPageIndex = 0; 423 } 424 425 setStatus(); 426 } 427 } 428 429 private Boolean m_blnIgnoreChange = false; 430 public Boolean IgnoreChange 431 { 432 get { return m_blnIgnoreChange; } 433 set { m_blnIgnoreChange = value; } 434 } 435 436 private Boolean m_blnShowLabel = true; 437 public Boolean ShowLabel 438 { 439 get { return m_blnShowLabel; } 440 set { m_blnShowLabel = value; labPageInfo.Visible = value; } 441 } 442 443 private void txtPageInfo_TextChanged(object sender, EventArgs e) 444 { 445 if (m_blnIgnTextChange) return; 446 m_blnIgnTextChange = true; 447 InputTextChanged(sender, e); 448 } 449 450 private void txtPageInfo_GotFocus(object sender, EventArgs e) 451 { 452 InputGotFocus(this, e); 453 } 454 455 private void txtPageInfo_LostFocus(object sender, EventArgs e) 456 { 457 InputLostFocus(this, e); 458 } 459 460 private void txtPageInfo_MouseDown(object sender, MouseEventArgs e) 461 { 462 InputMouseDown(sender, e); 463 } 464 465 private void txtPageInfo_MouseUp(object sender, MouseEventArgs e) 466 { 467 InputMouseUp(sender, e); 468 } 469 470 private void txtPageInfo_Click(object sender, EventArgs e) 471 { 472 InputTextClick(sender, e); 473 } 474 475 private void txtPageInfo_KeyDown(object sender, KeyEventArgs e) 476 { 477 InputKeyDown(sender, e); 478 } 479 480 private void txtPageInfo_KeyUp(object sender, KeyEventArgs e) 481 { 482 InputKeyUp(sender, e); 483 } 484 485 private void txtPageInfo_KeyPress(object sender, KeyPressEventArgs e) 486 { 487 InputKeyPress(sender, e); 488 } 489 490 private string m_strText = ""; 491 public String Text 492 { 493 get { return m_strText; } 494 set { m_strText = value; } 495 } 496 497 private System.Windows.Forms.Timer m_timerAutoPage = new Timer(); 498 private bool m_blnIsPrevDown = false; 499 private bool m_blnIsNextDown = false; 500 501 private bool m_blnIsBeginDown = false; 502 private bool m_blnIsEndDown = false; 503 504 private bool m_blnIsAutoJump = false; 505 public delegate void PageChangedHandle(int oldPage, int newPage, EventArgs e); 506 public event PageChangedHandle PageChanged; 507 508 private const int WAIT_FOR_AUTOJUMP = 500; 509 private const int AUTOJUMP_INV = 500; 510 511 private int m_intPageCount = 0; 512 protected String m_strErrorText = ""; 513 public String ErrorText 514 { 515 get { return m_strErrorText; } 516 set { m_strErrorText = value; } 517 } 518 519 /// 520 /// 键盘控件的父对象 521 /// 522 private Control m_keyboardParent = null; 523 public Control KeyboardParent 524 { 525 get { return m_keyboardParent; } 526 set { m_keyboardParent = value; } 527 } 528 529 /// 530 /// 是否显示输入翻页框按钮 531 /// 532 private Boolean m_blnShowTxtPageInfo = true; 533 public Boolean ShowTxtPageInfo 534 { 535 get { return m_blnShowTxtPageInfo; } 536 set { m_blnShowTxtPageInfo = value; txtPageInfo.Visible = value; } 537 } 538 539 //public bool IsPages { get; set; } 540 541 private bool m_blnIgnTextChange = false; 542 private Boolean m_blnIsInputFocus = false; 543 544 //public event EventHandler InputEnterPressed; 545 //public event EventHandler InputClearPressed; 546 547 public delegate void InputFocusHandle(object sender, EventArgs e); 548 public event InputFocusHandle InputGotFocus; 549 public event InputFocusHandle InputLostFocus; 550 551 public delegate void InputMouseHandle(object sender, MouseEventArgs e); 552 public event InputMouseHandle InputMouseDown; 553 public event InputMouseHandle InputMouseUp; 554 555 public event EventHandler InputTextClick; 556 557 public delegate void InputKeyHandle(object sender, KeyEventArgs e); 558 public event InputKeyHandle InputKeyDown; 559 public event InputKeyHandle InputKeyUp; 560 //public event InputKeyHandle InputTextKeyBoardEnter; 561 562 public delegate void InputKeyPressHandle(object sender, KeyPressEventArgs e); 563 public event InputKeyPressHandle InputKeyPress; 564 565 public event EventHandler InputTextChanged; 566 567 568 public TextBox InputTextBox 569 { 570 set { txtPageInfo=value; } 571 get { return txtPageInfo; } 572 } 573 574 public String InputText 575 { 576 get 577 { 578 if (m_strText == txtPageInfo.Text || String.IsNullOrWhiteSpace(txtPageInfo.Text)) 579 { 580 return String.Empty; 581 } 582 else 583 { 584 return txtPageInfo.Text; 585 } 586 } 587 set 588 { 589 if (m_blnIsInputFocus) 590 { 591 txtPageInfo.Text = value; 592 } 593 else 594 { 595 if (String.IsNullOrWhiteSpace(value)) 596 { 597 m_blnIgnTextChange = true; 598 txtPageInfo.Text = m_strText; 599 m_blnIgnTextChange = false; 600 } 601 } 602 } 603 } 604 605 public void setInputText(String text) 606 { 607 txtPageInfo.Text = text; 608 } 609 } 610 } 3. 你在资源管文件里面创建的control 文件夹里面,创建了一个用户控件ucPageTurn ,那么在工具栏里面会自动显示你刚刚写的ucPageTurn组件,把它拖到页面上 ,将这个组件的名字叫做ucPages,这样ucpageturn里面的控件就可以进行编辑 1 //在initControls里面添加textbox输入框的事件 2 private void initControls() 3 4 { 5 ucPages.InputTextBox.KeyPress +=new KeyPressEventHandler(ucPages_KeyPress); 6 ucPages.PageChanged += new Pku.CFM.Controls.ucPageTurn.PageChangedHandle(ucPages_PageChanged); 7 } 8 9 private void ucPages_KeyPress(object sender, KeyPressEventArgs e) 10 { 11 if (13 == e.KeyChar) 12 { 13 int intReturn = SysDefine.NOTHING; 14 String strInputText = ucPages.Text.ToUpper(); 15 if (SysDefine.FAILED == intReturn) 16 { 17 MessageBox.Show(this.ErrorText, "信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); 18 } 19 20 String pageInputText = ucPages.InputText; 21 int page = 0; 22 try 23 { 24 page = int.Parse(pageInputText); 25 } 26 catch 27 { 28 page = 1; 29 } 30 ucPages.CurPageIndex = page; 31 if (SysDefine.FAILED == refreshList(page, strInputText)) 32 { 33 MessageBox.Show(this.ErrorText, "信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); 34 } 35 } 36 } 37 38 private void ucPages_PageChanged(int oldPage, int newPage, EventArgs e) 39 { 40 int intReturn = SysDefine.NOTHING; 41 42 String strInputText = ucPages.Text.ToUpper(); 43 intReturn = refreshList(newPage, strInputText); 44 if (SysDefine.FAILED == intReturn) 45 { 46 MessageBox.Show(this.ErrorText, "信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); 47 return; 48 } 49 } View Code  C# winfrom 中自定义的翻页控件(自己设计)标签:val   点击   view   text   minimum   class   页面   dem   global   原文地址:http://www.cnblogs.com/anthonyboss/p/7509080.html

上一篇:aspect

下一篇:Delphi获取毫秒级时间戳


评论


亲,登录后才可以留言!