In this post we will see how to convert textbox text to upper case when it is entered .

For this we will consider an example, suppose a textbox is taken for full name, then we should allow only characters/letters in it. And we want full name to be entered in upper case. For this we can validate a textbox such that the text entered in it should appear in uppercase automatically even if user entered a lowercase.

C#.Net winforms textbox Uppercase

I am taking a textbox txtFullName on Form and a errorprovider. Now we are taking a keypress event of textbox txtFullName . The c# code is like this..

private void txtFullName_KeyPress(object sender, KeyPressEventArgs e)
{

    if (!char.IsLetter(e.KeyChar) && Convert.ToInt32(e.KeyChar) != 8 && Convert.ToInt32(e.KeyChar) != 32)
    {
         errorProvider1.SetError(txtFullName, "Only letters allowed");
         e.Handled = true;
         txtFullName.Focus();
    }
    else
    {
       errorProvider1.Clear();
       e.KeyChar = char.ToUpper(e.KeyChar);
    }
}
According to the above code, textbox allows only letters, backspace and space. And it converts the input text to uppercase even if user enters a lowercase letter .

If any other key like number or special character is entered by the user , the error provider will blink.

Leave a Reply

Your email address will not be published.