如何使 TextBox 只接受字母字符?

How to make a TextBox accept only alphabetic characters?(如何使 TextBox 只接受字母字符?)

本文介绍了如何使 TextBox 只接受字母字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让 TextBox 只接受带空格的字母字符?

How can I make a TextBox only accept alphabetic characters with spaces?

推荐答案

您可以使用以下代码段:

You could use the following snippet:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (!System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "^[a-zA-Z ]"))
    {
        MessageBox.Show("This textbox accepts only alphabetical characters");
        textBox1.Text.Remove(textBox1.Text.Length - 1);
    }
}

这篇关于如何使 TextBox 只接受字母字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使 TextBox 只接受字母字符?

基础教程推荐