[英]How to select listbox item by pressing enter key?
IDE: Visual studio 2010, c# .net 4.0, winforms appication: IDE:Visual Studio 2010,c#.net 4.0,winforms应用:
In my application I am showing list box. 在我的应用程序中,我正在显示列表框。 now I want to select a default item in list box [listbox selected index changed event will be called] ]when user presses enter key from keyboard.
现在,我想在列表框中选择一个默认项[当选中列表框更改索引的事件将被调用]]当用户按下键盘上的Enter键时。
Depending on where the Enter needs to be caught : 根据需要捕获Enter的位置:
I take the KeyUp event, valid events here are the KeyPress, KeyDown and KeyUp events 我接受KeyUp事件,这里的有效事件是KeyPress,KeyDown和KeyUp事件
public class YourFormOrControllerName : Form //Or UserControl
{
private int yourDefaultValue = 0;
public YourFormOrControllerName()
{
//whenever you haven't selected a specific control to setup (not a so good solution)
//YourFormOrControllerName.KeyUp += HandleKeyUpSelected;
//Whenever you Enter in the ListBox
YourListBoxControlName.KeyUp += HandleKeyUpSelected;
}
private void HandleKeyUpSelected(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
YourListBoxControlName.SelectedIndex = yourDefaultValue;
}
}
}
as @Nyerguds commented below there are different arguments for the different events. 正如@Nyerguds在下面评论的那样,针对不同事件有不同的论点。 For more information i refer to MSDN
有关更多信息,请参阅MSDN。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.