Character Casing in Combo

  • Thread starter Thread starter UmmagummA
  • Start date Start date
U

UmmagummA

How to make a ComboBox to display characters only in Upper or Lower Case as
you type?

I was trying to override OnKeyDown and OnKeyPress but coudn't figure it
out. The problem is that in OnKeyPress
new key value is not writed in the Combo.Text until the event is executed.
 
Hi UmmagummA,

What you do in the KeyPressEvent is set e.Handled = true to prevent the
characters ever reaching the ComboBox. Instead add a character of your
own.

Something like this might work for you, given a ComboBox b and its
KeyPressEvent b_Press

private void b_Press(object sender, KeyPressEventArgs e)
{
if(Char.IsLetter(e.KeyChar))
{
e.Handled = true;
string s = e.KeyChar.ToString();
b.Text += s.ToUpper();
b.SelectionStart = b.Text.Length;
}
}
 
Even better, using Char.ToUpper

if(Char.IsLetter(e.KeyChar))
{
e.Handled = true;
b.Text += Char.ToUpper(e.KeyChar);
b.SelectionStart = b.Text.Length;
}
 
Back
Top