使用TextBox控件的Textchanged事件
private void 控件名称_TextChanged(object sender, EventArgs e)
{
this.控件名称.TextChanged -= new System.EventHandler(this.控件名称_TextChanged);
StringBuilder sb = new StringBuilder();
int Selection = 控件名称.SelectionStart;
char s = '|';//用什么字符隔开
string str = 控件名称.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 - 控件名称.Text.Split(s).Length + 1;
控件名称.Text = sb.ToString();
控件名称.SelectionStart = Selection < 0 ? 0 : Selection;
this.控件名称.TextChanged += new System.EventHandler(this.控件名称_TextChanged);
}