c# set textBox text box can only enter letters

Let the text box only enter letters, otherwise a prompt box will be displayed.
1. It is realized through regular expressions:
matching a specific string:
  
Example: the validation method of the textBox3 component is set to input only uppercase letters

 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. Event KeyPress

judgment

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

Just do

Guess you like

Origin blog.csdn.net/ssdssa/article/details/109010399