P
P
pechenushka212019-05-28 11:15:41
C++ / C#
pechenushka21, 2019-05-28 11:15:41

How to enter only letters in textbox?

I can’t figure out how to enter only letters (I forbade spaces through keypress, but I need no characters and numbers to be entered)
here is the textbox code:

private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.Handled = (e.KeyChar == (char)Keys.Space);

    }
private void Button5_Click(object sender, EventArgs e)
    {

        if (textBox1.Text == "" ^  textBox1.Text.Any(char.IsDigit))
        {
            MessageBox.Show("Введите имя!\n1.Ваше имя не должно содержать цифр или любых других знаков!\n2.Ваше имя не должно быть пустым!");
        }



        else
        {
            //другие условия
        }




    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pechenushka21, 2019-05-28
@pechenushka21

string Symbol = e.KeyChar.ToString();

            if (!Regex.Match(Symbol, @"[а-яА-Я]|[a-zA-Z]").Success)
            {
                e.Handled = true;
            }

#
#, 2019-05-28
@mindtester

it is even better to use specially prepared textbox events - validation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question