bRaNdOn
bRaNdOn

Reputation: 1082

How to convert KeyEventArgs.KeyCode to Enum Keys

Hi im currently searching on how to convert the keycode in the event of Keydown in the KeyEventArgs of a certain control is there a way to do it? i've found a way to convert Keys Enum to Int I want it in the other way..

Upvotes: 0

Views: 1954

Answers (2)

Mark Hall
Mark Hall

Reputation: 54532

If what you are wanting is to take an int and convert it to a Keys Enumeration you can use the System.Enum.ToObject Method.

Keys key = (Keys)System.Enum.ToObject(typeof(Keys), 65 ); //Will be A

Though without seeing your code it is very hard to make a determination exactly what you are trying to do.

From above link:

Converts a specified integer value to an enumeration member.

Upvotes: 1

Rufus L
Rufus L

Reputation: 37020

You can simply use casting to get the Enum value of an int, although if you have KeyEventArgs then you already have both (KeyCode and KeyValue).

void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    // You can cast a Keys enum to an int 
    // (although you already have the int value in e.KeyValue)
    int keyInt = (int) e.KeyCode;

    // You can cast an int to a Keys enum 
    // (although you already have the enum value in e.KeyCode)
    Keys keyKeys = (Keys)keyInt;
}

Upvotes: 2

Related Questions