c#设置textBox文本框只能输入字母

让文本框只能输入字母,否则会显示提示框。
1、采用的是通过正则表达式实现的:
匹配特定字符串:
  
例:在textBox3组件的效验方法设置输入只能是大写字母

 private void textBox3_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (Regex.IsMatch(textBox3.Text, "^[A-Z]+$"))
                {
                }
                else
                {
                    MessageBox.Show("必须是大写字母");
                    textBox3.Clear();
                    textBox3.Focus();
                }
            }
            catch (FormatException)
            {
                MessageBox.Show("必须是大写字母");
                textBox3.Clear();
                textBox3.Focus();
            }
        }

2、事件KeyPress

判断

if(! char.IsLetter(e.KeyChar))
{
    e.Handled=true;
}

就行

猜你喜欢

转载自blog.csdn.net/ssdssa/article/details/109010399