c#列表框控件(箭头和输入键)

mee*_*eee 0 c# listbox

我有一个列表框,显示数组的内容.按下"go"按钮时,数组会填充一个结果列表.

go按钮在表单属性上设置为AcceptButton,因此在表单焦点的任何位置按Enter键会重新运行go按钮进程.

双击列表框中数组的结果可以正常使用如下:

void ListBox1_DoubleClick(object sender, EventArgs e) {}

我希望能够使用我的箭头键并输入键来选择和运行事件,而无需双击列表框中的行.(但是每次按钮都会运行)

基本打开表单,键入搜索字符串,按回车键运行go按钮,使用向上和向下箭头然后按Enter键选择运行相同的事件,如上面的双击.每一位之后都需要改变焦点.

key*_*rdP 8

您可以处理KeyDown要覆盖的控件的事件.例如,

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

}
Run Code Online (Sandbox Code Playgroud)

那将执行搜索.然后,您可以关注列表框

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

您可以Enter像上面的TextBox一样处理ListBox中的按钮并调用该DoubleClick事件.