C#在textBox中每输入两个字符自动添加一个特定字符
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.textBox1.TextChanged -= new System.EventHandler(this.textBox1_TextChanged);
StringBuilder sb = new StringBuilder();
int Selection = textBox1.SelectionStart;
char s = ' ';//用什么字符隔开
string str = textBox1.Text.Replace(s.ToString(), "");
for (int i = 1; i <= str.Length; i++)
{
sb.Append(str[i-1]);
if ((i != 0 && i % 2 == 0 ))//每组几个字符就%几
{
if (i == str.Length) continue;
sb.Append(s);
Selection++;
}
}
Selection = Selection - textBox1.Text.Split(s).Length + 1;
textBox1.Text = sb.ToString();
textBox1.SelectionStart = Selection < 0 ? 0 : Selection;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
}
版权声明:本文为qq_43024228原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。