How to convert string to uppercase in windows textbox?
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.
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'.
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);
}
Related videos on Youtube
Sukanya
Updated on March 07, 2020Comments
-
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 about 12 yearsno,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 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 theCharacterCasing
property. -
innoSPG about 10 yearsThe response is too long and can confuse the guy who ask. The question is actually clear and request only the event to handle.
-
Jim about 6 yearsIt should be the best answer!