joi, 25 martie 2010

How to get the string of KeyEventArgs.KeyValue?

I have a method in response to a KeyUp event of a control.

void m_control_KeyUp(object sender, KeyEventArgs e)
{
}

In oder to get the string of the key pressed you have to do something like this:

string _keyPressed = ((char)e.KeyValue).ToString();

But, this is not working all the time. For example if you press "+" you will get in _keyPress a value like ">>".

The solution is to use KeyPress event. This has a KeyPressEventArgs, that has a property KeyChar. Now, the code looks like:

void m_control_KeyPress(object sender, KeyPressEventArgs e)
{
string _keyPressed = e.KeyChar.ToString()
}





Niciun comentariu:

Trimiteți un comentariu