Hello guys! In my application i have textbox where a student has to enter his/her name.I have validated it so that it accepts only letters but the problem is if i want to delete something when typing i have to use the delete button only.I cant use the space tab as well...help me i am stuck. Here is my code

private void txtSur_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsLetter(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Enter Letters Only", "Invalid Character", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

Recommended Answers

All 2 Replies

Hello guys! In my application i have textbox where a student has to enter his/her name.I have validated it so that it accepts only letters but the problem is if i want to delete something when typing i have to use the delete button only.I cant use the space tab as well...help me i am stuck. Here is my code

private void txtSur_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsLetter(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Enter Letters Only", "Invalid Character", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

Oddly enough, I did something like this just the other day for digits:

private readonly string VALID_NUMBER_CHARS = "\b0123456789";

private void Numbers_KeyPress(object sender, KeyPressEventArgs e) {
    e.Handled = !VALID_NUMBER_CHARS.Contains(e.KeyChar);
}

The '\b' is included to enable more robust editing inside the control.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.