Thursday, September 29, 2011

textbox should accept only characters

1. write the code below at keypress event of textbox


private void txtFirstname_KeyPress(object sender, KeyPressEventArgs e)
        {


            if ((e.KeyChar >= 65 && e.KeyChar <= 90) || (e.KeyChar >= 97 && e.KeyChar <= 122) || (e.KeyChar == 32) || (e.KeyChar == 8))//keychar 8 is for backspace
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }


it should accept alphabets only.

No comments:

Post a Comment