MaskedIDNumber.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace LYFZ.ComponentLibrary
  10. {
  11. public partial class MaskedIDNumber : MaskedTextBox
  12. {
  13. public MaskedIDNumber()
  14. {
  15. InitializeComponent();
  16. }
  17. private string _strValue;
  18. /// <summary>
  19. /// 值
  20. /// </summary>
  21. public string StrValue
  22. {
  23. get
  24. {
  25. if (this.Text.Trim().Replace("-", "").Trim() != "")
  26. { _strValue = this.Text.Trim().Replace(" ", ""); }
  27. else
  28. { _strValue = this.Text.Trim().Replace("-", "").Trim(); }
  29. return _strValue;
  30. }
  31. set
  32. {
  33. _strValue = value;
  34. this.Text = _strValue;
  35. }
  36. }
  37. public MaskedIDNumber(IContainer container)
  38. {
  39. container.Add(this);
  40. InitializeComponent();
  41. this.Mask = "000000 00000000 000A";
  42. this.PromptChar = ' ';
  43. this.MouseClick += MaskedIDNumber_MouseClick;
  44. }
  45. void MaskedIDNumber_MouseClick(object sender, MouseEventArgs e)
  46. {
  47. if (this.StrValue.Length < 10)
  48. { this.ForCharMaskedTextBox(); }
  49. }
  50. /// <summary>
  51. /// 点击时在空值处获得光标
  52. /// </summary>
  53. private void ForCharMaskedTextBox()
  54. {
  55. char[] charList = this.Text.ToCharArray();
  56. bool isbl = true;
  57. for (int i = (charList.Length - 1); i >= 0; i--)
  58. {
  59. if (charList[i].ToString() != " ")
  60. {
  61. this.SelectionStart = i + 1;
  62. return;
  63. }
  64. if (i == 0)
  65. {
  66. if (isbl)
  67. { this.SelectionStart = 0; }
  68. }
  69. }
  70. }
  71. }
  72. }