MaskedTextBoxEx.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 MaskedTextBoxEx : MaskedTextBox
  11. {
  12. public MaskedTextBoxEx()
  13. {
  14. InitializeComponent();
  15. }
  16. public MaskedTextBoxEx(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. public void ForCharMaskedTextBox()
  46. {
  47. char[] charList = this.Text.Trim().ToCharArray();
  48. if (charList.Length > 0)
  49. {
  50. for (int i = (charList.Length - 1); i >= 0; i--)
  51. {
  52. if (i == 0)
  53. {
  54. if (charList[i].ToString() == " ")
  55. {
  56. this.SelectionStart = i;
  57. return;
  58. }
  59. }
  60. else
  61. {
  62. if (charList[i].ToString() != " ")
  63. {
  64. this.SelectionStart = i + 1;
  65. return;
  66. }
  67. }
  68. }
  69. }
  70. else { this.SelectionStart = 0; }
  71. }
  72. }
  73. }