MaskedTextTimeEx.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace LYFZ.ComponentLibrary
  9. {
  10. public partial class MaskedTextTimeEx : MaskedTextBox
  11. {
  12. public MaskedTextTimeEx()
  13. {
  14. InitializeComponent();
  15. }
  16. public MaskedTextTimeEx(IContainer container)
  17. {
  18. container.Add(this);
  19. InitializeComponent();
  20. this.MouseClick += MaskedTextBoxEx_MouseClick;
  21. }
  22. /// <summary>
  23. /// 鼠标点击事件
  24. /// </summary>
  25. /// <param name="sender"></param>
  26. /// <param name="e"></param>
  27. private void MaskedTextBoxEx_MouseClick(object sender, MouseEventArgs e)
  28. {
  29. ForCharMaskedTextBox();
  30. }
  31. /// <summary>
  32. /// 重写输入限制
  33. /// </summary>
  34. /// <param name="e"></param>
  35. protected override void OnKeyPress(KeyPressEventArgs e)
  36. {
  37. if (LYFZ.BLL.OtherCommonModel.IsQaJiaoChar(e.KeyChar))
  38. { e.Handled = true; }
  39. else
  40. { base.OnKeyPress(e); }
  41. }
  42. /// <summary>
  43. /// 点击时在空值处获得光标
  44. /// </summary>
  45. private void ForCharMaskedTextBox()
  46. {
  47. char[] charList = this.Text.ToCharArray();
  48. bool isbl = true;
  49. for (int i = (charList.Length - 1); i >= 0; i--)
  50. {
  51. if (i == 0)
  52. {
  53. if (charList[i].ToString() != ":" && charList[i].ToString() != " ")
  54. {
  55. this.SelectionStart = i + 1;
  56. return;
  57. }
  58. }
  59. else
  60. {
  61. if (charList[i].ToString() != ":" && charList[i].ToString() != " ")
  62. {
  63. this.SelectionStart = i + 1;
  64. return;
  65. }
  66. }
  67. if (i == 0)
  68. {
  69. if (isbl)
  70. { this.SelectionStart = 0; }
  71. }
  72. }
  73. }
  74. }
  75. }