如何使用回车键按下选择下拉列表的记录? [英] how can I select a record of a drop down list using enter key press?

查看:158
本文介绍了如何使用回车键按下选择下拉列表的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在keydown事件或按键事件上使用回车键选择下拉列表的记录(选定索引)。

我在这里展示我的作品..



I want to select a record (selected index) of a drop down list using enter key on keydown event or key press event.
here i am showing my works..

private void cmbItem_KeyDown(object sender, KeyEventArgs e)
       {
           try
           {
               if (e.KeyCode == Keys.Enter)
               {
                   // how I should select a record of a dropdown list in here??

               }

           }
           catch (Exception ex)
           {
               logger.Error(ex);
           }

       }

推荐答案

正如已经指出的,组合框下拉列表索引更改了事件参数不支持keypress KeyEventArgs。但是你可以做的是,为Selected索引添加一个事件处理程序,以调用另一个支持按键KeyEventArgs的子程序。



Combobox类: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged%28v=vs.110%29.aspx [ ^ ]



添加事件处理程序:http://msdn.microsoft.com/en-us/library/system.reflection.eventinfo.addeventhandler%28v=vs.110% 29.aspx [ ^ ]



这个链接是我提供的一个答案,就像我上面提到的那样:< br $> b $ b

如何在Vb中使用Sendkey模拟向下键.Net [ ^ ]



请记住,因为你希望的方式这个,您还需要使用SendMessage API,您可以在Pinvoke.Net或上面链接的解决方案中找到更多信息。



编辑:



我还想指出,我建议反对这种做法不切实际和不专业,因为有些特定的事件参数是专门为这些控件而创建的,你没有使用它们;应该是。简单地说,您不需要按键来进行此控制,因为用户将选择触发索引更改事件的项目。因此,无论您在按键操作中需要做什么,都可以在索引更改事件中完成。



问候。
As already pointed out, the combobox dropdown index changed event arguments do not support the keypress KeyEventArgs. But what you can do is, add an event handler to the Selected index changed to invoke another subroutine that supports the key press KeyEventArgs.

Combobox Class: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged%28v=vs.110%29.aspx[^]

Add Event Handler: http://msdn.microsoft.com/en-us/library/system.reflection.eventinfo.addeventhandler%28v=vs.110%29.aspx[^]

This link is an answer i provided on doing pretty much what I've suggested above:

How Can I Simulate The Down Key Using Sendkey In Vb.Net[^]

Keep in mind, because of the way you wish to do this, you will also need to make use of the SendMessage API which you can find out more about on Pinvoke.Net or in the above linked solution.



I would also like to point out that I advise against this approach as its impractical and unprofessional because there are specific event arguments created specifically for these controls which you are not using; and should be. Simply you do not need key press for this control because the user would be selecting an item firing the index changed event. So whatever you need to do in your keypress can be done in the index changed event.

Regards.


在你的代码中,我假设你有一个 ComboBox 名称为 myComboBox 。然后代码就像是,b $ b

In your code, I will assume that you're having a ComboBox with a name of myComboBox. Then the code would be like,

// inside the if else block
// get the selectedItem as a ComboBoxItem 
ComboBoxItem selectedItem = (myComboBox.SelectedItem as ComboBoxItem);





这样,一旦完成,变量 selectedItem 将具有所选的ComboBoxItem对象的值。



This way, once you're done, the variable selectedItem will have the value of the ComboBoxItem object that was selected.


这篇关于如何使用回车键按下选择下拉列表的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆