c# force uppercase lettering in a ComboBox
by brian on Apr.28, 2008, under .NET, GUI, c#, c# coding GUI, coding
I recently changed from a TextEdit control to a ComboBox and was somewhat annoyed that there wasn’t a property that forced the input characters to all be uppercase. So I dug around and came up with the following method using the KeyPress event.
private void productNumber_KeyPress(object sender, KeyPressEventArgs e)
{
char c = e.KeyChar;
// make it uppercase only
if (c >= 'a' && c <= 'z')
{
int digit = (int)c;
digit = digit - 'a' + 'A';
e.KeyChar = Convert.ToChar(digit);
}
}
When the event handler is called it gives you a chance to edit the hit character. Just by changing e.KeyChar you change the actual input character. This is a nifty trick to have under your belt.
