Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I capture the enter key in a windows forms combobox

Tags:

How do I capture the enter key in a windows forms combo box when the combobox is active?

I've tried to listen to KeyDown and KeyPress and I've created a subclass and overridden ProcessDialogKey, but nothing seems to work.

Any ideas?

/P

like image 413
Presidenten Avatar asked Aug 04 '09 10:08

Presidenten


People also ask

How do I bind a ComboBox in Windows form?

To bind a ComboBox or ListBox control If you are binding to a table, set the DisplayMember property to the name of a column in the data source. If you are binding to an IList, set the display member to a public property of the type in the list.

How do I select items in ComboBox?

When you set the SelectedItem property to an object, the ComboBox attempts to make that object the currently selected one in the list. If the object is found in the list, it is displayed in the edit portion of the ComboBox and the SelectedIndex property is set to the corresponding index.

Which property is used to add and access items in a ComboBox?

The Items property is used to add and access items in a ComboBox.


1 Answers

Hook up the KeyPress event to a method like this:

protected void myCombo_OnKeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == 13)
    {
        MessageBox.Show("Enter pressed", "Attention");                
    }
}

I've tested this in a WinForms application with VS2008 and it works.

If it isn't working for you, please post your code.

like image 176
Winston Smith Avatar answered Sep 17 '22 07:09

Winston Smith