4

I have a TextBox in my form and I added this event on it:

private void txtValue_KeyDown(object sender, KeyEventArgs e)
        {
            MessageBox.Show(e.KeyData.ToString());
        }

But it always prints the upper case of the letter even though I entered a lower case letter in the textBox. Please see image below:

my form

How should I get the right display? Thanks...

Timwi
  • 65,159
  • 33
  • 165
  • 230
yonan2236
  • 13,371
  • 33
  • 95
  • 141

2 Answers2

4

KeyDown and KeyUp use KeyEventArgs, which exposes the Keys enum via the KeyData property. The enum does not have representation for lower-case alphabetic values.

http://msdn.microsoft.com/en-us/library/system.windows.forms.keys.aspx

The KeyPress event allows you to get the actual character of the key pressed via KeyPressEventArgs.KeyChar.

private void txtValue_KeyPress(object sender, KeyPressEventArgs e)
{
    MessageBox.Show(e.KeyChar.ToString());
} 
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
3

If you handle the KeyPress event instead, you can examine the KeyChar property of KeyPressEventArgs to get the correct case.

Bradley Smith
  • 13,353
  • 4
  • 44
  • 57