IDE: Visual studio 2010, c# .net 4.0, winforms appication:
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.
Depending on where the Enter needs to be caught :
I take the KeyUp event, valid events here are the KeyPress, KeyDown and KeyUp events
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. For more information i refer to MSDN
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.