查看: 680|回复: 0

[ASP.NET教程] C# winfrom 中自定义的翻页控件(自己设计)

发表于 2017-9-27 08:00:00
尚学堂AD

1.主要是使用控件绑定点击事件

用到的控件分别为picturebox lable
上一页pbPage_Prev 下一页 pbPage_Next 首页 pbPage_Begin 尾页pbPage_End 是picturebox控件加背景图

“第 页/ 共 页” 是一个lable “labPageInfo” 在lable上面加了一个隐藏的textbox 控件 “txtPageInfo”

为什么要加一个textbox控件呢?实现的功能就是,lable会显示当前有共多少页和当前正在第几页,点击这个lable,隐藏的textbox会盖住前面的“第 页”,变成一个输入框,你输入数字,按回车就会跳到你指定的那一页去

2.将这个翻页的功能单独写在用户控件 ucPageTurn 里面 然后在每个页面直接应用就可以了

下面只是把ucPageTurn写了出来

  1. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.ComponentModel;
  4. 4 using System.Drawing;
  5. 5 using System.Data;
  6. 6 using System.Linq;
  7. 7 using System.Text;
  8. 8 using System.Windows.Forms;
  9. 9
  10. 10 namespace Demo
  11. 11 {
  12. 12 public partial class ucPageTurn : UserControl, IMessageFilter
  13. 13 {
  14. 14 private const int SIZE_HEIGHT = 40;
  15. 15 private const int SIZE_MINWIDTH = 84;
  16. 16 private const int SIZE_INFO_MINWIDTH = 188;
  17. 17
  18. 18 public ucPageTurn()
  19. 19 {
  20. 20 Application.AddMessageFilter(this);
  21. 21
  22. 22 InitializeComponent();
  23. 23 this.BorderStyle = System.Windows.Forms.BorderStyle.None;
  24. 24 this.MinimumSize = new Size(SIZE_MINWIDTH, 0);
  25. 25 this.Disposed += new EventHandler(ucPages_Disposed);
  26. 26
  27. 27 //this.MouseClick += new MouseEventHandler(ucKeyboard_Close);
  28. 28
  29. 29 PageChanged += new PageChangedHandle(new PageChangedHandle((oldPage, newPage, e) => { }));
  30. 30 InputGotFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { }));
  31. 31 //InputLostFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { }));
  32. 32
  33. 33 InputGotFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { }));
  34. 34 InputLostFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { }));
  35. 35
  36. 36 InputMouseDown += new InputMouseHandle(new InputMouseHandle((sender, e) => { }));
  37. 37 InputMouseUp += new InputMouseHandle(new InputMouseHandle((sender, e) => { }));
  38. 38 InputTextClick += new EventHandler(new EventHandler((sender, e) => { }));
  39. 39 InputKeyDown += new InputKeyHandle(new InputKeyHandle((sender, e) => { }));
  40. 40 InputKeyUp += new InputKeyHandle(new InputKeyHandle((sender, e) => { }));
  41. 41 InputKeyPress += new InputKeyPressHandle(new InputKeyPressHandle((sender, e) => { }));
  42. 42 InputTextChanged += new EventHandler(new EventHandler((sender, e) => { }));
  43. 43
  44. 44 this.BackColor = Color.White;
  45. 45 labPageInfo.BackColor = this.BackColor;
  46. 46
  47. 47 this.Resize += new EventHandler(ucPages_Resize);
  48. 48
  49. 49 //labPageInfo.MouseDoubleClick += new MouseEventHandler(labPageInfo_MouseDoubleClick);
  50. 50
  51. 51 pbPage_Prev.MouseDown += new MouseEventHandler(pbPage_Prev_MouseDown);
  52. 52 pbPage_Prev.MouseUp += new MouseEventHandler(pbPage_Prev_MouseUp);
  53. 53
  54. 54 pbPage_Next.MouseDown += new MouseEventHandler(pbPage_Next_MouseDown);
  55. 55 pbPage_Next.MouseUp += new MouseEventHandler(pbPage_Next_MouseUp);
  56. 56
  57. 57 pbPage_Begin.MouseDown += new MouseEventHandler(pbPage_Begin_MouseDown);
  58. 58 pbPage_Begin.MouseUp += new MouseEventHandler(pbPage_Begin_MouseUp);
  59. 59
  60. 60 pbPage_End.MouseDown += new MouseEventHandler(pbPage_End_MouseDown);
  61. 61 pbPage_End.MouseUp += new MouseEventHandler(pbPage_End_MouseUp);
  62. 62
  63. 63 txtPageInfo.TextChanged += new EventHandler(txtPageInfo_TextChanged);
  64. 64 txtPageInfo.GotFocus += new EventHandler(txtPageInfo_GotFocus);
  65. 65 txtPageInfo.Click += new EventHandler(txtPageInfo_Click);
  66. 66 txtPageInfo.Text = m_strText;
  67. 67 txtPageInfo.Visible = m_blnShowTxtPageInfo;
  68. 68
  69. 69 m_blnIsAutoJump = false;
  70. 70 m_timerAutoPage.Enabled = false;
  71. 71 m_timerAutoPage.Interval = WAIT_FOR_AUTOJUMP;
  72. 72 m_timerAutoPage.Tick += new EventHandler(timerAutoPage_Tick);
  73. 73 }
  74. 74
  75. 75 private void ucPages_Load(object sender, EventArgs e)
  76. 76 {
  77. 77 setStatus();
  78. 78 }
  79. 79
  80. 80 private void ucPages_Disposed(object sender, EventArgs e)
  81. 81 {
  82. 82 Application.RemoveMessageFilter(this);
  83. 83 }
  84. 84
  85. 85 public bool PreFilterMessage(ref System.Windows.Forms.Message MyMessage)
  86. 86 {
  87. 87 if (MyMessage.Msg == 0x204 || MyMessage.Msg == 0x205)
  88. 88 {
  89. 89 return true;
  90. 90 }
  91. 91 return false;
  92. 92 }
  93. 93
  94. 94 //设置控件的自适应大小
  95. 95 private void ucPages_Resize(object sender, EventArgs e)
  96. 96 {
  97. 97 this.Height = SIZE_HEIGHT;
  98. 98
  99. 99 pbPage_Begin.Location = new Point(0, 0);
  100. 100 pbPage_Begin.Size = new Size(SIZE_HEIGHT, SIZE_HEIGHT);
  101. 101
  102. 102 pbPage_Prev.Location = new Point(pbPage_Begin.Width + 2, pbPage_Begin.Top);
  103. 103 pbPage_Prev.Size = pbPage_Begin.Size;
  104. 104
  105. 105 pbPage_End.Location = new Point(this.Width - pbPage_End.Width, pbPage_Begin.Top);
  106. 106 pbPage_End.Size = pbPage_Begin.Size;
  107. 107
  108. 108 pbPage_Next.Location = new Point(this.Width - pbPage_Next.Width - pbPage_End.Width - 2, pbPage_Begin.Top);
  109. 109 pbPage_Next.Size = pbPage_Begin.Size;
  110. 110
  111. 111 if (this.Width < SIZE_INFO_MINWIDTH)
  112. 112 {
  113. 113 labPageInfo.Visible = false;
  114. 114 txtPageInfo.Visible = false;
  115. 115 }
  116. 116 else
  117. 117 {
  118. 118 labPageInfo.Location = new Point(pbPage_Prev.Width + pbPage_Prev.Width + 3, 2);
  119. 119 labPageInfo.Size = new Size(pbPage_Next.Left - labPageInfo.Left - 3, pbPage_Prev.Height);
  120. 120
  121. 121 txtPageInfo.Location = new Point(pbPage_Prev.Left + pbPage_Prev.Width + 5, 11);
  122. 122 //txtPageInfo.Size = new Size(pbPage_Next.Left - labPageInfo.Left , 15);
  123. 123
  124. 124 if (m_blnShowLabel && !labPageInfo.Visible) labPageInfo.Visible = true;
  125. 125 if (m_blnShowLabel && !txtPageInfo.Visible) txtPageInfo.Visible = true;
  126. 126 }
  127. 127
  128. 128 if (m_blnShowTxtPageInfo)
  129. 129 {
  130. 130 txtPageInfo.Size = new Size(79, labPageInfo.Height);
  131. 131 }
  132. 132 else
  133. 133 {
  134. 134 txtPageInfo.Size = new Size(0, labPageInfo.Height);
  135. 135 }
  136. 136 }
  137. 137
  138. 138 //点击lablelabPageInfo 显示txtPageInfo
  139. 139 private void labPageInfo_Click(object sender, EventArgs e)
  140. 140 {
  141. 141 if (!txtPageInfo.Visible)
  142. 142 {
  143. 143 showJumpPageStatus(true);
  144. 144 InputTextClick(txtPageInfo, new EventArgs());
  145. 145 }
  146. 146 else {
  147. 147 showJumpPageStatus(false);
  148. 148 }
  149. 149 }
  150. 150
  151. 151 public void showJumpPageStatus(Boolean isShow)
  152. 152 {
  153. 153 if (isShow)
  154. 154 {
  155. 155 txtPageInfo.Visible = true;
  156. 156 txtPageInfo.Text = string.Empty;
  157. 157 txtPageInfo.Focus();
  158. 158 }
  159. 159 else
  160. 160 {
  161. 161 txtPageInfo.Visible = false;
  162. 162 }
  163. 163 }
  164. 164
  165. 165 //上一页
  166. 166 private void pbPage_Prev_MouseDown(object sender, MouseEventArgs e)
  167. 167 {
  168. 168 pbPage_Prev.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Prev_D;
  169. 169 m_blnIsPrevDown = true;
  170. 170
  171. 171 m_timerAutoPage.Enabled = true;
  172. 172 }
  173. 173
  174. 174 private void pbPage_Prev_MouseUp(object sender, MouseEventArgs e)
  175. 175 {
  176. 176 pbPage_Prev.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Prev_N;
  177. 177 m_blnIsPrevDown = false;
  178. 178
  179. 179 if (m_blnIsAutoJump)
  180. 180 {
  181. 181 leaveAutoJumpMode();
  182. 182 return;
  183. 183 }
  184. 184 m_timerAutoPage.Enabled = false;
  185. 185
  186. 186 if (1 == m_intCurPageIndex) return;
  187. 187 int intOldPage = m_intCurPageIndex;
  188. 188 m_intCurPageIndex--;
  189. 189 setStatus();
  190. 190 PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());
  191. 191 }
  192. 192
  193. 193 //下一页
  194. 194 private void pbPage_Next_MouseDown(object sender, MouseEventArgs e)
  195. 195 {
  196. 196
  197. 197 pbPage_Next.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Next_D;
  198. 198 m_blnIsNextDown = true;
  199. 199
  200. 200 m_timerAutoPage.Enabled = true;
  201. 201 }
  202. 202
  203. 203 private void pbPage_Next_MouseUp(object sender, MouseEventArgs e)
  204. 204 {
  205. 205 pbPage_Next.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Next_N;
  206. 206 m_blnIsNextDown = false;
  207. 207
  208. 208 if (m_blnIsAutoJump)
  209. 209 {
  210. 210 leaveAutoJumpMode();
  211. 211 return;
  212. 212 }
  213. 213 m_timerAutoPage.Enabled = false;
  214. 214
  215. 215 if (m_intPageCount == m_intCurPageIndex) return;
  216. 216 int intOldPage = m_intCurPageIndex;
  217. 217 m_intCurPageIndex++;
  218. 218 setStatus();
  219. 219 PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());
  220. 220 }
  221. 221
  222. 222 //首页
  223. 223 private void pbPage_Begin_MouseDown(object sender, MouseEventArgs e)
  224. 224 {
  225. 225
  226. 226 pbPage_Begin.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_Begin_D;
  227. 227 m_blnIsBeginDown = false;
  228. 228 m_timerAutoPage.Enabled = true;
  229. 229 }
  230. 230
  231. 231 private void pbPage_Begin_MouseUp(object sender, MouseEventArgs e)
  232. 232 {
  233. 233 pbPage_Begin.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_Begin_N;
  234. 234 m_blnIsBeginDown = false;
  235. 235
  236. 236 int intOldPage = m_intCurPageIndex;
  237. 237
  238. 238 if (1 == m_intCurPageIndex) return;
  239. 239 m_intCurPageIndex = 1;
  240. 240
  241. 241
  242. 242 setStatus();
  243. 243 PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());
  244. 244
  245. 245 m_blnIsAutoJump = false;
  246. 246 m_timerAutoPage.Stop();
  247. 247 }
  248. 248
  249. 249 //尾页
  250. 250 private void pbPage_End_MouseDown(object sender, MouseEventArgs e)
  251. 251 {
  252. 252
  253. 253 pbPage_End.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_End_N;
  254. 254 m_blnIsEndDown = true;
  255. 255
  256. 256 m_timerAutoPage.Enabled = true;
  257. 257 }
  258. 258
  259. 259 private void pbPage_End_MouseUp(object sender, MouseEventArgs e)
  260. 260 {
  261. 261 pbPage_End.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_End_D;
  262. 262 m_blnIsEndDown = false;
  263. 263
  264. 264 int intOldPage = m_intCurPageIndex;
  265. 265
  266. 266 if (m_intCurPageIndex == m_intPageCount)return;
  267. 267 m_intCurPageIndex = m_intPageCount;
  268. 268
  269. 269 setStatus();
  270. 270 PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());
  271. 271
  272. 272 m_blnIsAutoJump = false;
  273. 273 m_timerAutoPage.Stop();
  274. 274
  275. 275 }
  276. 276
  277. 277 //翻页按钮的状态
  278. 278 private void setStatus()
  279. 279 {
  280. 280 //如果页数为0,翻页的按钮全部不显示
  281. 281 if (m_intPageCount <= 0)
  282. 282 {
  283. 283 labPageInfo.Text = "";
  284. 284 pbPage_Prev.Visible = false;
  285. 285 pbPage_Next.Visible = false;
  286. 286 pbPage_Begin.Visible = false;
  287. 287 pbPage_End.Visible = false;
  288. 288 txtPageInfo.Visible = false;
  289. 289 }
  290. 290 else
  291. 291 {
  292. 292 //如果页数为1,翻页的按钮不显示
  293. 293 if (1 == m_intPageCount)
  294. 294 {
  295. 295 labPageInfo.Text = "";
  296. 296 pbPage_Prev.Visible = false;
  297. 297 pbPage_Next.Visible = false;
  298. 298 pbPage_Begin.Visible = false;
  299. 299 pbPage_End.Visible = false;
  300. 300 txtPageInfo.Visible = false;
  301. 301 }
  302. 302 else
  303. 303 {
  304. 304 //只显示下一页和最后一页的按钮
  305. 305 if (m_intCurPageIndex <= 1)
  306. 306 {
  307. 307 m_intCurPageIndex = 1;
  308. 308
  309. 309 pbPage_Prev.Visible = false;
  310. 310 pbPage_Next.Visible = true;
  311. 311 pbPage_Begin.Visible = false;
  312. 312 pbPage_End.Visible = true;
  313. 313 txtPageInfo.Visible = false;
  314. 314
  315. 315 }
  316. 316 //只显示上一页和首页的按钮
  317. 317 else if (m_intCurPageIndex >= m_intPageCount)
  318. 318 {
  319. 319 m_intCurPageIndex = m_intPageCount;
  320. 320
  321. 321 pbPage_Prev.Visible = true;
  322. 322 pbPage_Next.Visible = false;
  323. 323 pbPage_Begin.Visible = true ;
  324. 324 pbPage_End.Visible = false;
  325. 325 txtPageInfo.Visible = false;
  326. 326
  327. 327 }
  328. 328 //否则按钮全部显示
  329. 329 else
  330. 330 {
  331. 331 pbPage_Prev.Visible = true;
  332. 332 pbPage_Next.Visible = true;
  333. 333 pbPage_Begin.Visible = true;
  334. 334 pbPage_End.Visible = true;
  335. 335 txtPageInfo.Visible = false;
  336. 336 }
  337. 337
  338. 338 labPageInfo.Text = String.Format("第{0}页 /共小贝页", m_intCurPageIndex, m_intPageCount);
  339. 339 txtPageInfo.Text = String.Format("{0}", m_intCurPageIndex);
  340. 340 }
  341. 341 }
  342. 342 }
  343. 343
  344. 344 private void timerAutoPage_Tick(object sender, EventArgs e)
  345. 345 {
  346. 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. 347 {
  348. 348 leaveAutoJumpMode();
  349. 349 return;
  350. 350 }
  351. 351
  352. 352 if (!m_blnIsAutoJump) m_timerAutoPage.Interval = AUTOJUMP_INV;
  353. 353
  354. 354 int intOldPage = m_intCurPageIndex;
  355. 355
  356. 356 if (m_blnIsNextDown) m_intCurPageIndex++;
  357. 357 if (m_blnIsPrevDown) m_intCurPageIndex--;
  358. 358
  359. 359 setStatus();
  360. 360
  361. 361 PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());
  362. 362
  363. 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. 364 {
  365. 365 leaveAutoJumpMode();
  366. 366 }
  367. 367 else
  368. 368 {
  369. 369 m_blnIsAutoJump = true;
  370. 370 }
  371. 371 }
  372. 372
  373. 373 private void leaveAutoJumpMode()
  374. 374 {
  375. 375 m_blnIsAutoJump = false;
  376. 376 m_timerAutoPage.Stop();
  377. 377 m_timerAutoPage.Enabled = false;
  378. 378 m_timerAutoPage.Interval = WAIT_FOR_AUTOJUMP;
  379. 379 }
  380. 380
  381. 381 private int m_intCurPageIndex = 0;
  382. 382
  383. 383 //当前页面
  384. 384 public int CurPageIndex
  385. 385 {
  386. 386 get { return m_intCurPageIndex; }
  387. 387 set
  388. 388 {
  389. 389 if (value < 0 || (m_intPageCount> 0 && value > m_intPageCount)) return;
  390. 390
  391. 391 int intOldPage = m_intCurPageIndex;
  392. 392 m_intCurPageIndex = value;
  393. 393
  394. 394 setStatus();
  395. 395 if (!m_blnIgnoreChange) PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());
  396. 396
  397. 397 m_blnIsAutoJump = false;
  398. 398 m_timerAutoPage.Stop();
  399. 399 m_timerAutoPage.Enabled = false;
  400. 400 }
  401. 401 }
  402. 402
  403. 403 //计算总页数
  404. 404 public int PageCount
  405. 405 {
  406. 406 get { return m_intPageCount; }
  407. 407 set
  408. 408 {
  409. 409 m_intPageCount = value;
  410. 410
  411. 411 if (m_intPageCount > 0)
  412. 412 {
  413. 413 if (m_intCurPageIndex <= 0) m_intCurPageIndex = 0;
  414. 414
  415. 415 if (m_intCurPageIndex > m_intPageCount)
  416. 416 {
  417. 417 m_intCurPageIndex = m_intPageCount;
  418. 418 }
  419. 419 }
  420. 420 else
  421. 421 {
  422. 422 m_intCurPageIndex = 0;
  423. 423 }
  424. 424
  425. 425 setStatus();
  426. 426 }
  427. 427 }
  428. 428
  429. 429 private Boolean m_blnIgnoreChange = false;
  430. 430 public Boolean IgnoreChange
  431. 431 {
  432. 432 get { return m_blnIgnoreChange; }
  433. 433 set { m_blnIgnoreChange = value; }
  434. 434 }
  435. 435
  436. 436 private Boolean m_blnShowLabel = true;
  437. 437 public Boolean ShowLabel
  438. 438 {
  439. 439 get { return m_blnShowLabel; }
  440. 440 set { m_blnShowLabel = value; labPageInfo.Visible = value; }
  441. 441 }
  442. 442
  443. 443 private void txtPageInfo_TextChanged(object sender, EventArgs e)
  444. 444 {
  445. 445 if (m_blnIgnTextChange) return;
  446. 446 m_blnIgnTextChange = true;
  447. 447 InputTextChanged(sender, e);
  448. 448 }
  449. 449
  450. 450 private void txtPageInfo_GotFocus(object sender, EventArgs e)
  451. 451 {
  452. 452 InputGotFocus(this, e);
  453. 453 }
  454. 454
  455. 455 private void txtPageInfo_LostFocus(object sender, EventArgs e)
  456. 456 {
  457. 457 InputLostFocus(this, e);
  458. 458 }
  459. 459
  460. 460 private void txtPageInfo_MouseDown(object sender, MouseEventArgs e)
  461. 461 {
  462. 462 InputMouseDown(sender, e);
  463. 463 }
  464. 464
  465. 465 private void txtPageInfo_MouseUp(object sender, MouseEventArgs e)
  466. 466 {
  467. 467 InputMouseUp(sender, e);
  468. 468 }
  469. 469
  470. 470 private void txtPageInfo_Click(object sender, EventArgs e)
  471. 471 {
  472. 472 InputTextClick(sender, e);
  473. 473 }
  474. 474
  475. 475 private void txtPageInfo_KeyDown(object sender, KeyEventArgs e)
  476. 476 {
  477. 477 InputKeyDown(sender, e);
  478. 478 }
  479. 479
  480. 480 private void txtPageInfo_KeyUp(object sender, KeyEventArgs e)
  481. 481 {
  482. 482 InputKeyUp(sender, e);
  483. 483 }
  484. 484
  485. 485 private void txtPageInfo_KeyPress(object sender, KeyPressEventArgs e)
  486. 486 {
  487. 487 InputKeyPress(sender, e);
  488. 488 }
  489. 489
  490. 490 private string m_strText = "";
  491. 491 public String Text
  492. 492 {
  493. 493 get { return m_strText; }
  494. 494 set { m_strText = value; }
  495. 495 }
  496. 496
  497. 497 private System.Windows.Forms.Timer m_timerAutoPage = new Timer();
  498. 498 private bool m_blnIsPrevDown = false;
  499. 499 private bool m_blnIsNextDown = false;
  500. 500
  501. 501 private bool m_blnIsBeginDown = false;
  502. 502 private bool m_blnIsEndDown = false;
  503. 503
  504. 504 private bool m_blnIsAutoJump = false;
  505. 505 public delegate void PageChangedHandle(int oldPage, int newPage, EventArgs e);
  506. 506 public event PageChangedHandle PageChanged;
  507. 507
  508. 508 private const int WAIT_FOR_AUTOJUMP = 500;
  509. 509 private const int AUTOJUMP_INV = 500;
  510. 510
  511. 511 private int m_intPageCount = 0;
  512. 512 protected String m_strErrorText = "";
  513. 513 public String ErrorText
  514. 514 {
  515. 515 get { return m_strErrorText; }
  516. 516 set { m_strErrorText = value; }
  517. 517 }
  518. 518
  519. 519 /// <summary>
  520. 520 /// 键盘控件的父对象
  521. 521 /// </summary>
  522. 522 private Control m_keyboardParent = null;
  523. 523 public Control KeyboardParent
  524. 524 {
  525. 525 get { return m_keyboardParent; }
  526. 526 set { m_keyboardParent = value; }
  527. 527 }
  528. 528
  529. 529 /// <summary>
  530. 530 /// 是否显示输入翻页框按钮
  531. 531 /// </summary>
  532. 532 private Boolean m_blnShowTxtPageInfo = true;
  533. 533 public Boolean ShowTxtPageInfo
  534. 534 {
  535. 535 get { return m_blnShowTxtPageInfo; }
  536. 536 set { m_blnShowTxtPageInfo = value; txtPageInfo.Visible = value; }
  537. 537 }
  538. 538
  539. 539 //public bool IsPages { get; set; }
  540. 540
  541. 541 private bool m_blnIgnTextChange = false;
  542. 542 private Boolean m_blnIsInputFocus = false;
  543. 543
  544. 544 //public event EventHandler InputEnterPressed;
  545. 545 //public event EventHandler InputClearPressed;
  546. 546
  547. 547 public delegate void InputFocusHandle(object sender, EventArgs e);
  548. 548 public event InputFocusHandle InputGotFocus;
  549. 549 public event InputFocusHandle InputLostFocus;
  550. 550
  551. 551 public delegate void InputMouseHandle(object sender, MouseEventArgs e);
  552. 552 public event InputMouseHandle InputMouseDown;
  553. 553 public event InputMouseHandle InputMouseUp;
  554. 554
  555. 555 public event EventHandler InputTextClick;
  556. 556
  557. 557 public delegate void InputKeyHandle(object sender, KeyEventArgs e);
  558. 558 public event InputKeyHandle InputKeyDown;
  559. 559 public event InputKeyHandle InputKeyUp;
  560. 560 //public event InputKeyHandle InputTextKeyBoardEnter;
  561. 561
  562. 562 public delegate void InputKeyPressHandle(object sender, KeyPressEventArgs e);
  563. 563 public event InputKeyPressHandle InputKeyPress;
  564. 564
  565. 565 public event EventHandler InputTextChanged;
  566. 566
  567. 567
  568. 568 public TextBox InputTextBox
  569. 569 {
  570. 570 set { txtPageInfo=value; }
  571. 571 get { return txtPageInfo; }
  572. 572 }
  573. 573
  574. 574 public String InputText
  575. 575 {
  576. 576 get
  577. 577 {
  578. 578 if (m_strText == txtPageInfo.Text || String.IsNullOrWhiteSpace(txtPageInfo.Text))
  579. 579 {
  580. 580 return String.Empty;
  581. 581 }
  582. 582 else
  583. 583 {
  584. 584 return txtPageInfo.Text;
  585. 585 }
  586. 586 }
  587. 587 set
  588. 588 {
  589. 589 if (m_blnIsInputFocus)
  590. 590 {
  591. 591 txtPageInfo.Text = value;
  592. 592 }
  593. 593 else
  594. 594 {
  595. 595 if (String.IsNullOrWhiteSpace(value))
  596. 596 {
  597. 597 m_blnIgnTextChange = true;
  598. 598 txtPageInfo.Text = m_strText;
  599. 599 m_blnIgnTextChange = false;
  600. 600 }
  601. 601 }
  602. 602 }
  603. 603 }
  604. 604
  605. 605 public void setInputText(String text)
  606. 606 {
  607. 607 txtPageInfo.Text = text;
  608. 608 }
  609. 609 }
  610. 610 }
复制代码

3. 下面把ucpageturn写到的翻页功能应用到窗体上,
你在资源管文件里面创建的control 文件夹里面,创建了一个用户控件ucPageTurn ,

那么在工具栏里面会自动显示你刚刚写的ucPageTurn组件,把它拖到页面上 ,将这个组件的名字叫做ucPages,这样ucpageturn里面的控件就可以进行编辑

  1. 1 //在initControls里面添加textbox输入框的事件
  2. 2 private void initControls()
  3. 3
  4. 4 {
  5. 5 ucPages.InputTextBox.KeyPress +=new KeyPressEventHandler(ucPages_KeyPress);
  6. 6 ucPages.PageChanged += new Pku.CFM.Controls.ucPageTurn.PageChangedHandle(ucPages_PageChanged);
  7. 7 }
  8. 8
  9. 9 private void ucPages_KeyPress(object sender, KeyPressEventArgs e)
  10. 10 {
  11. 11 if (13 == e.KeyChar)
  12. 12 {
  13. 13 int intReturn = SysDefine.NOTHING;
  14. 14 String strInputText = ucPages.Text.ToUpper();
  15. 15 if (SysDefine.FAILED == intReturn)
  16. 16 {
  17. 17 MessageBox.Show(this.ErrorText, "信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  18. 18 }
  19. 19
  20. 20 String pageInputText = ucPages.InputText;
  21. 21 int page = 0;
  22. 22 try
  23. 23 {
  24. 24 page = int.Parse(pageInputText);
  25. 25 }
  26. 26 catch
  27. 27 {
  28. 28 page = 1;
  29. 29 }
  30. 30 ucPages.CurPageIndex = page;
  31. 31 if (SysDefine.FAILED == refreshList(page, strInputText))
  32. 32 {
  33. 33 MessageBox.Show(this.ErrorText, "信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  34. 34 }
  35. 35 }
  36. 36 }
  37. 37
  38. 38 private void ucPages_PageChanged(int oldPage, int newPage, EventArgs e)
  39. 39 {
  40. 40 int intReturn = SysDefine.NOTHING;
  41. 41
  42. 42 String strInputText = ucPages.Text.ToUpper();
  43. 43 intReturn = refreshList(newPage, strInputText);
  44. 44 if (SysDefine.FAILED == intReturn)
  45. 45 {
  46. 46 MessageBox.Show(this.ErrorText, "信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  47. 47 return;
  48. 48 }
  49. 49 }
复制代码
View Code



回复

使用道具 举报