getting keyboard input in winform listbox

273 views Asked by At

i am altering a program so that a listbox picks up keyboard input (number pad, D0-D9, letter keys) and edits properties of the currently selected listbox item based on the input. my problem is that there seems to be no functionality that can convert a key press into a letter. for examples, currently i have a function that goes

        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; }

and so on for the entire alphabet and the numbers, and i would like to expand it to include symbols as well. i was wondering if a more efficient function exists i can call that can take in a keyeventArg, or its keyCode / KeyValue / KeyData and be able to tell me what letter/ number it is, instead of typing it out for each individual letter/number/symbol.

1

There are 1 answers

0
Michael Rudner Evanchik On BEST ANSWER
    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();
    }