22

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

Presidenten
  • 6,327
  • 11
  • 45
  • 55

7 Answers7

26

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.

Lucas Jones
  • 19,767
  • 8
  • 75
  • 88
Winston Smith
  • 21,585
  • 10
  • 60
  • 75
24

In case you define AcceptButton on your form, you cannot listen to Enter key in KeyDown/KeyUp/KeyPress.

In order to check for that, you need to override ProcessCmdKey on FORM:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
    if ((this.ActiveControl == myComboBox) && (keyData == Keys.Return)) {
        MessageBox.Show("Combo Enter");
        return true;
    } else {
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

In this example that would give you message box if you are on combo box and it works as before for all other controls.

Josip Medved
  • 3,631
  • 1
  • 28
  • 36
13

or altertatively you can hook up the KeyDown event:

private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        MessageBox.Show("Enter pressed.");
    }
}
Petros
  • 8,862
  • 3
  • 39
  • 38
1
private void comboBox1_KeyDown( object sender, EventArgs e )
{
   if( e.KeyCode == Keys.Enter )
   {
      // Do something here...
   } else Application.DoEvents();
}
jay_t55
  • 11,362
  • 28
  • 103
  • 174
1

Try this:

protected override bool ProcessCmdKey(ref Message msg, Keys k)
{
    if (k == Keys.Enter || k == Keys.Return)
    {
        this.Text = null;
        return true;
    }

    return base.ProcessCmdKey(ref msg, k);
}
Jay
  • 179
  • 1
  • 3
0

It could be that your dialog has a button that's eating the enter key because it's set to be the AcceptButton in the form property.
If that's the case then you solve this like this by unsetting the AcceptButton property when the control gets focus then resetting it back once the control loses focus ( in my code, button1 is the accept button )

private void comboBox1_Enter(object sender, EventArgs e)
{
    this.AcceptButton = null;
}

private void comboBox1_Leave(object sender, EventArgs e)
{
    this.AcceptButton = button1;
}

private void comboBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Enter)
        {
            MessageBox.Show("Hello");
        }
    }

I have to admit not liking my own solution as it seems a little hacky to unset/set the AcceptButton property so if anyone has a better solution then I'd be interested

zebrabox
  • 5,694
  • 1
  • 28
  • 32
0
protected void Form_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == 13)  // or Key.Enter or Key.Return
    {
        MessageBox.Show("Enter pressed", "KeyPress Event");                
    }
}

Don't forget to set KeyPreview to true on the form.