是的,这当然有可能。您可以添加一个验证事件处理程序来检查字符。<您可以有一个允许字符的字典,或者如果您将字符限制为某种编码(可能是 UTF-8),则可以使用和将该字符与一系列字符进行比较>。
更具体地说:您可以处理该KeyPress事件。如果e.KeyChar无效,则设置e.Handled为true。
尝试这个:
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (System.Text.Encoding.UTF8.GetByteCount(new char[] { e.KeyChar }) > 1)
{
e.Handled = true;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2246 次 |
| 最近记录: |