How to convert KeyCode to a string?

  • Thread starter Thread starter BD
  • Start date Start date
B

BD

Hi,

I need to convert a KeyCode from event KeyDown to a string. For
example Keys.Decimal that's 110 to ".", but it converts to char 'n'
that's the code ascii of 110. I'm a little confuse with this.

[]'s
BD
 
Hi,

I need to convert a KeyCode from event KeyDown to a string. For
example Keys.Decimal that's 110 to ".", but it converts to char 'n'
that's the code ascii of 110. I'm a little confuse with this.

[]'s
BD

Just cast KeyEventArgs.KeyValue to a char and assign it to the string, for
example:

private string s = string.Empty;
//
private void SomeKeyDown(object sender, KeyPressEventArgs e)
{
s += (char)e.KeyValue;
}

Regards,
Anne
 
Hi,

I need to convert a KeyCode from event KeyDown to a string. For
example Keys.Decimal that's 110 to ".", but it converts to char 'n'
that's the code ascii of 110. I'm a little confuse with this.

[]'s
BD

Just cast KeyEventArgs.KeyValue to a char and assign it to the string, for
example:

private string s = string.Empty;
//
private void SomeKeyDown(object sender, KeyPressEventArgs e)
{
s += (char)e.KeyValue;
}

Regards,
Anne

But I'm using the Form_KeyDown(object sender, KeyEventArgs e)
and the string representation of the decimal key "." is a "n".

s += (char)e.KeyValue; // s = "n" and I click the decimal key

Why it's so different?
How can I get the decimal character from the Regional Settings?

[]'s
BD
 
But I'm using the Form_KeyDown(object sender, KeyEventArgs e)
and the string representation of the decimal key "." is a "n".

s += (char)e.KeyValue; // s = "n" and I click the decimal key

Why it's so different?
How can I get the decimal character from the Regional Settings?

KeyEventArgs contains the key number of the keyboard , while
KeyPressEventArgs contains the key pressed (see
http://msdn2.microsoft.com/en-us/library/system.windows.forms.keyeventargs.aspx
for detailed information). So you better use Form_KeyPress instead of
Form_KeyDown;

The decimal character (and more info) can be obtained from class
NumberFormatInfo (using System.Globalization). In the next example all keys
will be send to the control that has focus (like a textbox), but not if the
decimal seperator is pressed:

private void Form_KeyPress(object sender, KeyPressEventArg e)
{
if (NumberFormatInfo.CurrentInfo.NumberDecimalSeperator ==
e.KeyChar.ToString())
{
// Decimal seperator pressed
e.Handled = true; // don't send the character to the focused control
}
}

Regards,
Anne
 
KeyEventArgs contains the key number of the keyboard , while
KeyPressEventArgs contains the key pressed (see
http://msdn2.microsoft.com/en-us/library/system.windows.forms.keyeventargs.aspx
for detailed information). So you better use Form_KeyPress instead of
Form_KeyDown;

The decimal character (and more info) can be obtained from class
NumberFormatInfo (using System.Globalization). In the next example all keys
will be send to the control that has focus (like a textbox), but not if the
decimal seperator is pressed:

private void Form_KeyPress(object sender, KeyPressEventArg e)
{
if (NumberFormatInfo.CurrentInfo.NumberDecimalSeperator ==
e.KeyChar.ToString())
{
// Decimal seperator pressed
e.Handled = true; // don't send the character to the focused control
}
}

Regards,
Anne

Understood. Thank you very much Anne.

[]'s
BD
 
Back
Top