How to convert string to uppercase in windows textbox?

59,166

Solution 1

You just need to change CharacterChasing property to Upper.

textBox1.CharacterCasing = CharacterCasing.Upper

Solution 2

Why to reinvent the wheel, just set 'CharacterCasing' property of textBox to 'Upper'. You don't need to write any code.

Make letters in textBox uppercase

In case of masked textbox, you can use '>' (in mask property) to make following characters uppercase. e.g. For a input alphanumeric string (A-Z, 0-9) of length eight, use mask '>AAAAAAAA'. To restrict to letters only (A-Z), use '>LLLLLLLL'.

Make letters in maskedTextBox uppercase

Solution 3

You need to assign the results of ToUpper back to the textbox:

txtBox.Text = txtBox.Text.ToUpper();

Alternatively, set the CharacterCasing property of the textbox to Upper:

txtBox.CharacterCasing = CharacterCasing.Upper;

Solution 4

In properties of TextBox simply set CharacterCasing to Upper. It'll convert all entered character in uppercase.

Solution 5

private void mytextbox_KeyPress(object sender, KeyPressEventArgs e)

{

e.KeyChar = Char.ToUpper(e.KeyChar);

}
Share:
59,166

Related videos on Youtube

Sukanya
Author by

Sukanya

Updated on March 07, 2020

Comments

  • Sukanya
    Sukanya about 4 years

    I've a textbox in my windows application. It allows only alphabets and digits. I want when ever I type any alphabet, it should be converted to uppercase.How can I do that and in which event? I've used str.ToUpper() but the cursor is shifting to the beginning of the string. Please give me solution.

  • Sukanya
    Sukanya about 12 years
    no,doing so in keypress or keydown or keyup event shifts the cursor at the zero position in the textbox as I mentioned in my question.
  • Oded
    Oded about 12 years
    @Sukanya - Indeed. Which is why you shouldn't do that on those event handlers. You can do this on the LostFocus event handler, but need to be careful, but a better solution is to simply set the CharacterCasing property.
  • innoSPG
    innoSPG about 10 years
    The response is too long and can confuse the guy who ask. The question is actually clear and request only the event to handle.
  • Jim
    Jim about 6 years
    It should be the best answer!