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 < SIZE_INFO_MINWIDTH) 112 { 113 labPageInfo.Visible = false; 114 txtPageInfo.Visible = false; 115 } 116 else 117 { 118 labPageInfo.Location = new Point(pbPage_Prev.Width + pbPage_Prev.Width + 3, 2); 119 labPageInfo.Size = new Size(pbPage_Next.Left - labPageInfo.Left - 3, pbPage_Prev.Height); 120 121 txtPageInfo.Location = new Point(pbPage_Prev.Left + pbPage_Prev.Width + 5, 11); 122 //txtPageInfo.Size = new Size(pbPage_Next.Left - labPageInfo.Left , 15); 123 124 if (m_blnShowLabel && !labPageInfo.Visible) labPageInfo.Visible = true; 125 if (m_blnShowLabel && !txtPageInfo.Visible) txtPageInfo.Visible = true; 126 } 127 128 if (m_blnShowTxtPageInfo) 129 { 130 txtPageInfo.Size = new Size(79, labPageInfo.Height); 131 } 132 else 133 { 134 txtPageInfo.Size = new Size(0, labPageInfo.Height); 135 } 136 } 137 138 //点击lablelabPageInfo 显示txtPageInfo 139 private void labPageInfo_Click(object sender, EventArgs e) 140 { 141 if (!txtPageInfo.Visible) 142 { 143 showJumpPageStatus(true); 144 InputTextClick(txtPageInfo, new EventArgs()); 145 } 146 else { 147 showJumpPageStatus(false); 148 } 149 } 150 151 public void showJumpPageStatus(Boolean isShow) 152 { 153 if (isShow) 154 { 155 txtPageInfo.Visible = true; 156 txtPageInfo.Text = string.Empty; 157 txtPageInfo.Focus(); 158 } 159 else 160 { 161 txtPageInfo.Visible = false; 162 } 163 } 164 165 //上一页 166 private void pbPage_Prev_MouseDown(object sender, MouseEventArgs e) 167 { 168 pbPage_Prev.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Prev_D; 169 m_blnIsPrevDown = true; 170 171 m_timerAutoPage.Enabled = true; 172 } 173 174 private void pbPage_Prev_MouseUp(object sender, MouseEventArgs e) 175 { 176 pbPage_Prev.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Prev_N; 177 m_blnIsPrevDown = false; 178 179 if (m_blnIsAutoJump) 180 { 181 leaveAutoJumpMode(); 182 return; 183 } 184 m_timerAutoPage.Enabled = false; 185 186 if (1 == m_intCurPageIndex) return; 187 int intOldPage = m_intCurPageIndex; 188 m_intCurPageIndex--; 189 setStatus(); 190 PageChanged(intOldPage, m_intCurPageIndex, new EventArgs()); 191 } 192 193 //下一页 194 private void pbPage_Next_MouseDown(object sender, MouseEventArgs e) 195 { 196 197 pbPage_Next.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Next_D; 198 m_blnIsNextDown = true; 199 200 m_timerAutoPage.Enabled = true; 201 } 202 203 private void pbPage_Next_MouseUp(object sender, MouseEventArgs e) 204 { 205 pbPage_Next.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Next_N; 206 m_blnIsNextDown = false; 207 208 if (m_blnIsAutoJump) 209 { 210 leaveAutoJumpMode(); 211 return; 212 } 213 m_timerAutoPage.Enabled = false; 214 215 if (m_intPageCount == m_intCurPageIndex) return; 216 int intOldPage = m_intCurPageIndex; 217 m_intCurPageIndex++; 218 setStatus(); 219 PageChanged(intOldPage, m_intCurPageIndex, new EventArgs()); 220 } 221 222 //首页 223 private void pbPage_Begin_MouseDown(object sender, MouseEventArgs e) 224 { 225 226 pbPage_Begin.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_Begin_D; 227 m_blnIsBeginDown = false; 228 m_timerAutoPage.Enabled = true; 229 } 230 231 private void pbPage_Begin_MouseUp(object sender, MouseEventArgs e) 232 { 233 pbPage_Begin.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_Begin_N; 234 m_blnIsBeginDown = false; 235 236 int intOldPage = m_intCurPageIndex; 237 238 if (1 == m_intCurPageIndex) return; 239 m_intCurPageIndex = 1; 240 241 242 setStatus(); 243 PageChanged(intOldPage, m_intCurPageIndex, new EventArgs()); 244 245 m_blnIsAutoJump = false; 246 m_timerAutoPage.Stop(); 247 } 248 249 //尾页 250 private void pbPage_End_MouseDown(object sender, MouseEventArgs e) 251 { 252 253 pbPage_End.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_End_N; 254 m_blnIsEndDown = true; 255 256 m_timerAutoPage.Enabled = true; 257 } 258 259 private void pbPage_End_MouseUp(object sender, MouseEventArgs e) 260 { 261 pbPage_End.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_End_D; 262 m_blnIsEndDown = false; 263 264 int intOldPage = m_intCurPageIndex; 265 266 if (m_intCurPageIndex == m_intPageCount)return; 267 m_intCurPageIndex = m_intPageCount; 268 269 setStatus(); 270 PageChanged(intOldPage, m_intCurPageIndex, new EventArgs()); 271 272 m_blnIsAutoJump = false; 273 m_timerAutoPage.Stop(); 274 275 } 276 277 //翻页按钮的状态 278 private void setStatus() 279 { 280 //如果页数为0,翻页的按钮全部不显示 281 if (m_intPageCount <= 0) 282 { 283 labPageInfo.Text = ""; 284 pbPage_Prev.Visible = false; 285 pbPage_Next.Visible = false; 286 pbPage_Begin.Visible = false; 287 pbPage_End.Visible = false; 288 txtPageInfo.Visible = false; 289 } 290 else 291 { 292 //如果页数为1,翻页的按钮不显示 293 if (1 == m_intPageCount) 294 { 295 labPageInfo.Text = ""; 296 pbPage_Prev.Visible = false; 297 pbPage_Next.Visible = false; 298 pbPage_Begin.Visible = false; 299 pbPage_End.Visible = false; 300 txtPageInfo.Visible = false; 301 } 302 else 303 { 304 //只显示下一页和最后一页的按钮 305 if (m_intCurPageIndex <= 1) 306 { 307 m_intCurPageIndex = 1; 308 309 pbPage_Prev.Visible = false; 310 pbPage_Next.Visible = true; 311 pbPage_Begin.Visible = false; 312 pbPage_End.Visible = true; 313 txtPageInfo.Visible = false; 314 315 } 316 //只显示上一页和首页的按钮 317 else if (m_intCurPageIndex >= m_intPageCount) 318 { 319 m_intCurPageIndex = m_intPageCount; 320 321 pbPage_Prev.Visible = true; 322 pbPage_Next.Visible = false; 323 pbPage_Begin.Visible = true ; 324 pbPage_End.Visible = false; 325 txtPageInfo.Visible = false; 326 327 } 328 //否则按钮全部显示 329 else 330 { 331 pbPage_Prev.Visible = true; 332 pbPage_Next.Visible = true; 333 pbPage_Begin.Visible = true; 334 pbPage_End.Visible = true; 335 txtPageInfo.Visible = false; 336 } 337 338 labPageInfo.Text = String.Format("第{0}页 /共{1}页", m_intCurPageIndex, m_intPageCount); 339 txtPageInfo.Text = String.Format("{0}", m_intCurPageIndex); 340 } 341 } 342 } 343 344 private void timerAutoPage_Tick(object sender, EventArgs e) 345 { 346 if ((m_blnIsNextDown && m_intCurPageIndex >= m_intPageCount) || (m_blnIsPrevDown && m_intCurPageIndex <= 1) || (m_blnIsEndDown && m_intCurPageIndex >=m_intPageCount) || (m_blnIsBeginDown && m_intCurPageIndex<=1)) 347 { 348 leaveAutoJumpMode(); 349 return; 350 } 351 352 if (!m_blnIsAutoJump) m_timerAutoPage.Interval = AUTOJUMP_INV; 353 354 int intOldPage = m_intCurPageIndex; 355 356 if (m_blnIsNextDown) m_intCurPageIndex++; 357 if (m_blnIsPrevDown) m_intCurPageIndex--; 358 359 setStatus(); 360 361 PageChanged(intOldPage, m_intCurPageIndex, new EventArgs()); 362 363 if ((m_blnIsNextDown && m_intCurPageIndex >= m_intPageCount) || (m_blnIsPrevDown && m_intCurPageIndex <= 1)||(m_blnIsEndDown && m_intCurPageIndex >= m_intPageCount) || (m_blnIsBeginDown && m_intCurPageIndex <= 1)) 364 { 365 leaveAutoJumpMode(); 366 } 367 else 368 { 369 m_blnIsAutoJump = true; 370 } 371 } 372 373 private void leaveAutoJumpMode() 374 { 375 m_blnIsAutoJump = false; 376 m_timerAutoPage.Stop(); 377 m_timerAutoPage.Enabled = false; 378 m_timerAutoPage.Interval = WAIT_FOR_AUTOJUMP; 379 } 380 381 private int m_intCurPageIndex = 0; 382 383 //当前页面 384 public int CurPageIndex 385 { 386 get { return m_intCurPageIndex; } 387 set 388 { 389 if (value < 0 || (m_intPageCount> 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 <= 0) m_intCurPageIndex = 0; 414 415 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 }