0

I have a listbox which displays the contents of an array. The array is populated with a list of results when my "go" button is pressed.

The go button is set as the AcceptButton on the form properties so pressing the Enter key anywhere in the focus of the form re-runs the go button process.

Double clicking on a result from the array within the listbox works fine using below:

void ListBox1_DoubleClick(object sender, EventArgs e) {}

I would like to be able to use my arrow keys and enter keys to select and run an event without having to double click on the line within the listbox. (however go button runs each time instead)

Basically open the form, type search string, press enter to run go button, use up and down arrows then press enter on selection to run same event as double click above. Will need to change focus after each bit.

Jay
  • 5,897
  • 1
  • 25
  • 28
meeeeeeeeee
  • 311
  • 4
  • 8
  • 18

2 Answers2

8

You can handle the KeyDown events for the controls you want to override. For example,

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        //execute go button method
        GoButtonMethod();
        //or if it's an event handler (should be a method)
        GoButton_Click(null,null);
    }

}

That will perform the search. You can then focus your listbox

myListBox.Focus();
//you might need to select one value to allow arrow keys
myListBox.SelectedIndex = 0;

You can handle the Enter button in the ListBox the same way as the TextBox above and call the DoubleClick event.

keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • Thanks for this, I have used the mylistbox.focus(); at the end of the go script so the up and down arrows can cycle through the list box but pressing enter just reruns the go button. I have tried to implement listbox1_keydown() as shown above but I cant seem to declare it with this.listBox1.keydown += new System.EventHandler(this.ListBox1_KeyDown); as it says no overload for listobx1_keydown – meeeeeeeeee Jan 06 '12 at 15:07
  • You need to change the `textbox1_KeyDown` code from above and replace the `GoButton_Click/GoButtonMethod` with your `ListBox1_DoubleClick(ListBox1,null)` when checking for the `Enter` button in the ListBox. – keyboardP Jan 06 '12 at 15:09
  • Thanks, I am trying to do this and edited the post above, when I add the code and try to declare with this.listBox1.keydown += new System.EventHandler(this.ListBox1_KeyDown); I get no overload for listbox1_keydown – meeeeeeeeee Jan 06 '12 at 15:23
  • No overload for 'ListBox1_KeyDown' matches delegate 'System.EventHandler – meeeeeeeeee Jan 06 '12 at 15:24
  • It has a KeyDown event. http://msdn.microsoft.com/en-us/library/system.windows.uielement.keydown%28v=VS.95%29.aspx `listBox1.KeyDown += new KeyEventHandler(listBox1_KeyDown);` – keyboardP Jan 06 '12 at 15:29
2

This problem is similar to - Pressing Enter Key will Add the Selected Item From ListBox to RichTextBox

Certain controls do not recognize some keys when they are pressed in Control::KeyDown event. For e.g. list box does not recognize if the key pressed is Enter key.

See the remarks section of the Control::KeyDown event reference.

One way to resolve your problem might be writing a method for the Control::PreviewKeyDown event for your list box control:

private void listBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Up && this.listBox1.SelectedIndex - 1 > -1)
    {
        //listBox1.SelectedIndex--;
    }
    if (e.KeyCode == Keys.Down && this.listBox1.SelectedIndex + 1 < this.listBox1.Items.Count)
    {
        //listBox1.SelectedIndex++;
    }
    if (e.KeyCode == Keys.Enter)
    {
        //Do your task here :)
    }
}

private void listBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Enter:
            e.IsInputKey = true;
            break;
    }
}
Community
  • 1
  • 1