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)
But in practice, this does not work inside a
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))
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.
Find the solution here.
My blog in Spanish