[英]How to select listbox item by pressing enter key?
IDE:Visual Studio 2010,c#.net 4.0,winforms應用:
在我的應用程序中,我正在顯示列表框。 現在,我想在列表框中選擇一個默認項[當選中列表框更改索引的事件將被調用]]當用戶按下鍵盤上的Enter鍵時。
根據需要捕獲Enter的位置:
我接受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;
}
}
}
正如@Nyerguds在下面評論的那樣,針對不同事件有不同的論點。 有關更多信息,請參閱MSDN。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.