C# Winform TextBox 限制输入个数 汉字 两个

如题所述

设置TextBox的KeyPress事件,加入下面代码:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// 此代码为了使Backspace按键在输入2字之后还能有效
if (e.KeyChar == (char)Keys.Back)
{
e.Handled = false;
return;
}
if (textBox1.Text.Length >= 2) e.Handled = true;
}
这样就只能输入2个汉字(或者英文字母)了。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-03-04
textBox 有对应的属性设置:MaxLength,计算出对应汉字所占的字符数就行
相似回答