converting a string to Keys in C#

拜拜、爱过 提交于 2021-01-27 14:56:57

问题


Lets say we store KeyCode value as a string. How do you convert it back to KeyCode?

For example, I've captured a key on keydown event:

string modifier = e.Modifiers.ToString(); // Control
string key_string = e.KeyCode.ToString(); // D1

How to do the following ?

Keys old_key_restored = (Keys)key_string;

Code above doesn't work.

EDIT: Daniel is a life savior ;)

Keys key_restored = (Keys) Enum.Parse(typeof(Keys), key_key);

回答1:


Its just an enum so you can use Enum.TryParse



来源:https://stackoverflow.com/questions/12060513/converting-a-string-to-keys-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!