Click to See Complete Forum and Search --> : [RESOLVED] Force entered text to uppercase


Chuck Bruce
June 21st, 2006, 03:26 PM
Try as I might, I have not been able to do this! I am trying to force text entered into a ComboBox to uppercase. I don't want to block the entering of lowercase letters, just automatically convert them to uppercase. I have tried creating a class derived from ComboBox, and have used a myriad of combinations of the KeyPress, KeyDown and KeyUp methods, to no avail.

Surely someone has done this before and can point me in the right direction.

PLEASE!

Vanaj
June 21st, 2006, 03:43 PM
Did you try setting the combobox styles to uppercase ?

Chuck Bruce
June 21st, 2006, 05:31 PM
I was not able to find a way to do so. There is a property in the TextBox for doing that, but I do not see one for the ComboBox. Perhaps I am overlooking something, or is this something I must do programmatically?

Thanks for the reply.

Vanaj
June 21st, 2006, 07:49 PM
I was not able to find a way to do so. There is a property in the TextBox for doing that, but I do not see one for the ComboBox. Perhaps I am overlooking something, or is this something I must do programmatically?

Thanks for the reply.Should be under the "Styles" tab of the ComboBox "Properties" as I recall...but then again I am getting older and memory doesn't work like it does in the computer.

Sheesh
June 21st, 2006, 08:56 PM
Its under the Behavior category, this is called the CharacterCasing property that converts a TextBox to L/UCase. The ComboBox to my knowledge doesn't support this property, at least not in V03 which is what I am using. Just M$ being stupid, :D ...So you have to do it with code.

Here is one solution that converts them all at once.


private void comboBox1_Leave(object sender, System.EventArgs e)
{
comboBox1.Text = comboBox1.Text.ToUpper();
}


Here is a solution that will work as you type. Make sure you put the code in the correct events or it will not work correctly. You cannot use the KeyUp Event with this code. You have to use the KeyDown event.


bool keyHandled=false;
private void comboBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (keyHandled)e.Handled=true;
keyHandled=false;
return;
}

private void comboBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if ((e.KeyCode >= Keys.A)&&(e.KeyCode <= Keys.Z))
{
comboBox1.SelectedText=e.KeyCode.ToString().ToUpper();
keyHandled=true;
}
return;
}


If you are using V05 it may be supported now, so you may want to check before you use this code. If your using V03 you don't have any choice but to do it manually.

aniskhan
June 22nd, 2006, 12:02 AM
try thisprivate void ComboBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (char.IsLower(e.KeyChar)) {
e.KeyChar = char.ToUpper(e.KeyChar);
}
}

Chuck Bruce
June 22nd, 2006, 09:39 AM
Thanks guys for the replies!

My solution was a slight modification of the second solution proposed by Sheesh.


bool keyHandled=false;
private void comboBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
e.Handled=keyHandled;
}

private void comboBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if ((e.KeyCode >= Keys.A)&&(e.KeyCode <= Keys.Z))
{
comboBox1.SelectedText=e.KeyCode.ToString().ToUpper();
keyHandled=true;
}
else keyHandled = false;
}


This works just as I need it to. The else statement I added was required to keep it from ignoring any non-alphabetic characters entered (otherwise the first alphabet character causes the keyHandled flag to be true forever more).

Hard to believe it was that easy after all the different ways I tried. Thanks Sheesh! :D

As for aniskhan's reply: I had tried that already, but e.KeyChar is a read only value (at least in VS2003 which is what I am using). But thanks for the reply anyway. :wave:

Sheesh
June 22nd, 2006, 05:03 PM
Glad you got it working. I never encountered the issue, I just tried it again so I'm not sure what problem you were having. The code in the keypress event always set the value back to false and keypress was always fired. It looks like you changed the keypress event a little and thats why it stopped working. But you came up with a good solution there. I like it better then mine.

Anyway, as long as you got it working.

aniskhan
June 23rd, 2006, 03:37 PM
Sorry Chuck Bruce i coded it in vs2005

aniskhan
June 23rd, 2006, 03:52 PM
can do it like
Char chr;
private void comboBox1_KeyPress(.......
{
if (char.IsLetter(e.KeyChar))
{
chr = char.ToUpper(e.KeyChar);
e.Handled = true;
this.TextBox1.SelectedText += chr;
}
}

Sheesh
June 23rd, 2006, 08:25 PM
@aniskhan

That's a nice solution. Its much cleaner then mine. Nice job. :thumb: