在winform列表框中获取键盘输入

时间:2015-06-15 16:24:06

标签: c# winforms keyevent

我正在改变程序,以便列表框选取键盘输入(数字键盘,D0-D9,字母键)并根据输入编辑当前所选列表框项目的属性。我的问题是似乎没有可以将按键转换为字母的功能。例如,目前我有一个功能

        string letter;
        if (e.KeyCode == Keys.A) { letter = "a"; return letter; }
        if (e.KeyCode == Keys.B) { letter = "b"; return letter; }
        if (e.KeyCode == Keys.C) { letter = "c"; return letter; }
        if (e.KeyCode == Keys.D) { letter = "d"; return letter; }
        if (e.KeyCode == Keys.E) { letter = "e"; return letter; }
        if (e.KeyCode == Keys.F) { letter = "f"; return letter; }
        if (e.KeyCode == Keys.G) { letter = "g"; return letter; }
        if (e.KeyCode == Keys.H) { letter = "h"; return letter; }
        if (e.KeyCode == Keys.I) { letter = "i"; return letter; }
        if (e.KeyCode == Keys.J) { letter = "j"; return letter; }
        if (e.KeyCode == Keys.K) { letter = "k"; return letter; }
        if (e.KeyCode == Keys.L) { letter = "l"; return letter; }
        if (e.KeyCode == Keys.M) { letter = "m"; return letter; }
        if (e.KeyCode == Keys.N) { letter = "n"; return letter; }
        if (e.KeyCode == Keys.O) { letter = "o"; return letter; }
        if (e.KeyCode == Keys.P) { letter = "p"; return letter; }

依此类推整个字母和数字,我想扩展它以包含符号。我想知道是否存在一个更有效的函数我可以调用它可以接受一个keyeventArg,或它的keyCode / KeyValue / KeyData,并能够告诉我它是什么字母/数字,而不是为每个单独的字母/数字键入它/符号。

1 个答案:

答案 0 :(得分:0)

    private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        string letter = "";
        //letter = ProcessKey(e.KeyChar);
        //or
        letter = e.KeyChar.ToString();
    }

    private string ProcessKey(char key)
    {
        return key.ToString();
    }