效果:

代码:
private void textBox1_TextChanged(object sender, EventArgs e)
{
string content = this.textBox1.Text;
int length = content.Length;
if(length>=5 && length<=7)
{
string temp = "";
if(length == 5)
{
temp = content.Replace(content.Substring(4, 1), "*");
}else if(length == 6)
{
temp = content.Replace(content.Substring(5, 1), "*");
}else if(length == 7)
{
temp = content.Replace(content.Substring(6, 1), "*");
}
this.textBox1.Text = temp;
textBox1.SelectionStart = textBox1.TextLength;//设置文本框选定的文本起点
}
}
文本框输入限制与替换
本文介绍了一个简单的C#程序,该程序用于实现在文本框中输入特定长度字符串时自动进行字符替换的功能。当文本框中的字符串长度达到5到7个字符时,程序会将最后一个字符替换为星号(*)。
160

被折叠的 条评论
为什么被折叠?



