Click here to Skip to main content
13,865,647 members
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 25 Jul 2017
Licenced CPOL

How Do I Capture the Enter Key in a Windows Forms Combobox

, 25 Jul 2017
Rate this:
Please Sign up or sign in to vote.
Quick tip for capture ENTER key in a Windows Form combobox

Difficult Problems, Easy Solutions

Today, I had problems again with the issue of executing a function when pressing the ENTER key inside a ComboBox in a Windows Forms application using C #.

The first thing you think, just like in a TextBox is to use the KeyPress Event and use the same code as in it, something like:

if ((int)e.KeyChar == (int)Keys.Enter)
    //code here

But in practice, this does not work inside a ComboBox.

The Solution

As it is not the first time that I face this, on this occasion, I wanted to try to solve it in the simplest way possible, since on other occasions the truth is that I read a lot. But I got the solution.

You have to overwrite the ProcessCmdKey event and check that the active control is the ComboBox you want. This, in theory, you could do with all the controls on the form.

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

As you see if the active control is checked, so that by putting there the control you want to execute a function when pressing ENTER.

Happy coding!

Find the solution here.

My blog in Spanish


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sergi Ortiz Gomez
Software Developer DrUalcman
Philippines Philippines
Programador en c#, VB, JAVASCRPT, HTML, ASP. CSS para web, windows form, windows app.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190214.1 | Last Updated 26 Jul 2017
Article Copyright 2017 by Sergi Ortiz Gomez
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid