代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Text.RegularExpressions; 6 using System.Windows.Controls; 7 using System.Windows.Input; 8 9 namespace DriverEasyWPF.Utils 10 { 11 public class LicenseTextBox : TextBox 12 { 13 private bool isDelete = false; 14 public int BasicLength { get; set; }//每段长度 15 public int SumCount { get; set; }//总共几段 16 public String SpChar { get; set; }//分隔的符号 17 public bool ShowSpCharFirst { get; set; } 18 protected override void OnTextChanged(TextChangedEventArgs e) 19 { 20 base.OnTextChanged(e); 21 22 if (ShowSpCharFirst) 23 Do(); 24 else 25 Do2(); 26 27 } 28 29 private void Do() 30 { 31 //SetCaretIndex(); 32 CheckWhetherDelete(); 33 34 InitData(); 35 /* 36 * eg: 12345-12345-12345-12345-12345 37 */ 38 if (!isDelete) 39 { 40 for (int i = 0; i < SumCount - 1; i++) 41 { 42 int length = BasicLength * (i + 1); 43 AppendSubtractByLength(length + i); 44 IgnoreInputSubtract(length + (i + 2), SpChar); 45 } 46 int maxLength = BasicLength * SumCount + (SumCount - 1); 47 if (Text.Length >= maxLength || (Text.Length >= SumCount*BasicLength && Text.IndexOf(SpChar)<=0)) 48 { 49 //Text = Text.Substring(0, maxLength);//旧代码 50 //Regex reg = new Regex("[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}"); 51 //原型是:^([A-Z0-9]{{0}}-){4}[A-Z0-9]{{5}}$ 52 String regPattern = "^([A-Z0-9]{"+BasicLength+"}-){"+(SumCount-1)+"}[A-Z0-9]{"+BasicLength+"}$"; 53 Regex reg = new Regex(regPattern); 54 if (!reg.IsMatch(Text)) 55 { 56 String content = Text.Replace(SpChar, String.Empty); 57 Text = content.Substring(0, BasicLength) + SpChar 58 + content.Substring(BasicLength, BasicLength) + SpChar 59 + content.Substring(BasicLength * 2, BasicLength) + SpChar 60 + content.Substring(BasicLength * 3, BasicLength) + SpChar 61 + content.Substring(BasicLength * 4, BasicLength); 62 } 63 SetCaretIndex(); 64 } 65 } 66 } 67 68 private void Do2() 69 { 70 //SetCaretIndex(); 71 CheckWhetherDelete(); 72 73 InitData(); 74 /* 75 * eg: 12345-12345-12345-12345-12345 76 */ 77 if (!isDelete) 78 { 79 for (int i = 0; i < SumCount - 1; i++) 80 { 81 int length = BasicLength * (i + 1); 82 EqualsWithoutSpChar(length + i); 83 EqualsWithSpChar(length + i); 84 } 85 86 #region old logic 87 /* 88 if (Text.Length == 6 && Text[5].ToString().Equals(SpChar)) 89 Text = Text; 90 if (Text.Length == 6 && !Text[5].ToString().Equals(SpChar)) 91 { 92 Text = Text.Substring(0, 5) + SpChar + Text.Substring(5); 93 SetCaretIndex(); 94 } 95 96 if (Text.Length == 12 && Text[11].ToString().Equals(SpChar)) 97 Text = Text; 98 if (Text.Length == 12 && !Text[11].ToString().Equals(SpChar)) 99 { 100 Text = Text.Substring(0, 11) + SpChar + Text.Substring(11); 101 SetCaretIndex(); 102 } 103 104 if (Text.Length == 18 && Text[17].ToString().Equals(SpChar)) 105 Text = Text; 106 if (Text.Length == 18 && !Text[17].ToString().Equals(SpChar)) 107 { 108 Text = Text.Substring(0, 17) + SpChar + Text.Substring(17); 109 SetCaretIndex(); 110 } 111 112 if (Text.Length == 24 && Text[23].ToString().Equals(SpChar)) 113 Text = Text; 114 if (Text.Length == 24 && !Text[23].ToString().Equals(SpChar)) 115 { 116 Text = Text.Substring(0, 23) + SpChar + Text.Substring(23); 117 SetCaretIndex(); 118 } 119 */ 120 #endregion 121 122 int maxLength = BasicLength * SumCount + (SumCount - 1); 123 if (Text.Length > maxLength) 124 { 125 Text = Text.Substring(0, maxLength); 126 SetCaretIndex(); 127 } 128 } 129 } 130 131 private void EqualsWithSpChar(int length) 132 { 133 if (Text.Length == (length+1) && !Text[length].ToString().Equals(SpChar)) 134 { 135 Text = Text.Substring(0, length) + SpChar + Text.Substring(length); 136 SetCaretIndex(); 137 } 138 } 139 140 private void EqualsWithoutSpChar(int length) 141 { 142 if (Text.Length == (length+1) && Text[length].ToString().Equals(SpChar)) 143 Text = Text; 144 } 145 146 /// <summary> 147 /// 默认是 5段,每段5个长度 148 /// </summary> 149 private void InitData() 150 { 151 if (BasicLength <= 0) 152 BasicLength = 5; 153 154 if (SumCount <= 0) 155 SumCount = 5; 156 157 if (String.IsNullOrEmpty(SpChar)) 158 SpChar = "-"; 159 160 } 161 162 private void CheckWhetherDelete() 163 { 164 if (Keyboard.IsKeyDown(Key.Delete) || Keyboard.IsKeyDown(Key.Back)) 165 isDelete = true; 166 else 167 isDelete = false; 168 } 169 170 private void SetCaretIndex() 171 { 172 //this.CaretIndex = Text.Length; 173 this.Focus(); 174 this.SelectionStart = Text.Length; 175 } 176 177 private void IgnoreInputSubtract(int length, String subChar) 178 { 179 if (Text.Length == length && Text[length - 1].ToString() == subChar) 180 { 181 Text = Text.Substring(0, length - 1); 182 SetCaretIndex(); 183 } 184 } 185 186 private void AppendSubtractByLength(int length) 187 { 188 if (Text.Length == length) 189 { 190 Text += SpChar; 191 SetCaretIndex(); 192 } 193 } 194 195 196 } 197 }
使用:
1 <!--现在xaml的文件头,添加相应的命名空间--> 2 xmlns:model="clr-namespace:LicenseKeyTextBoxDemo.Model" 3 4 <!--用法如下--> 5 <model:LicenseTextBox Margin="88,220,70,255" BasicLength="5" SumCount="5" SpChar="-" IsMSMode="true"/>
想法:
改进: