0

I have a combobox with some useful items. Operators can select items from this list. I also want to make it possible that the operator types some text that is not in the selection list.

If the operator indicates that he finished editing the textbox, for instance by pressing enter, I want to check if the operator selected an item, or typed some new text.

From Best way to get selected item OR entered text from combobox I learned that I can read the textbox text like this:

var operatorText = comboBox1.Txt;

But what event should I use?

  • SelectedIndexChanged is not raised if I type some text and press enter
  • TextUpdate is raised for every character that the operator types, but not if he presses enter
  • TestUpdated is raised for every character change, even after selections, but not if enter is pressed.

I hear a "ding" when I press enter after editing the textbox, so some event should occur, but which one?

Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
  • What about using the [PreviewKeyDown Event](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.previewkeydown?view=net-5.0)? You could check for the presence of the "enter" and or "return" key and perform what you need to do. – Trevor Apr 22 '21 at 13:22
  • 1
    In ComboBox.KeyDown: `if (e.KeyCode == Keys.Enter) { e.SuppressKeyPress = true; string text = comboBox1.Text; }` – Jimi Apr 22 '21 at 13:25
  • 1
    Does this answer your question? [How do I capture the enter key in a windows forms combobox](https://stackoverflow.com/questions/1226726/how-do-i-capture-the-enter-key-in-a-windows-forms-combobox) – Sinatr Apr 22 '21 at 13:28
  • Then you can do, e.g., `bool exists = comboBox1.Items.OfType().Any(itm => comboBox1.GetItemText(itm).Equals(text));`. -- If you have a Custom Control, you can get the handle of the Edit control ([GetComboBoxInfo()](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getcomboboxinfo)) and use a NativeWindow to handle all its messages (and raise custom events, if needed). – Jimi Apr 22 '21 at 13:30
  • The solution comes from Jimi: (Thanks Jimi!) Although PreviewKeyDown event also can check on Keys.Enter, it is the e.SuppressKeyPress that prevents the "Ding". For future readers I'll add it as an answer, and pay tribute to Jimi – Harald Coppoolse Apr 22 '21 at 13:40

2 Answers2

1

Jimi came up with the correct answer. Subscribe to event Control.KeyDown (not KeyUp!), check which key is pressed, and if you think that the operator has finished editing, SuppressKeyPress, to prevent the "Ding"

private void OnParameterKeyDown(object sender, KeyEventArgs e)
{
    ComboBox combo = (ComboBox)sender;
    if (e.KeyCode == Keys.Return)
    {
        this.ProcessSelectedText(combo.Text);
        e.SuppressKeyPress = true;
    }
}

I chose not to react on LostFocus: if an operator starts typing, but sees that he made a mistake, he should be able to do other things instead, for instance lookup the correct text to type

Another method could be that if the operator presses enter you could add the typed text to the items list, so that the item will be selected. Disadvantage: the item list might become quite large, and it will be a nuisance to see typing errors in your list.

Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
0

does it have to be enter? If it's just when the users moves from the textfield to a new field, you could use LostFocus (https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.lostfocus?view=net-5.0)

  • It doesn't have to be enter, but that is the most natural way for an operator. Moving the mouse away is not enough, for LostFocus you have to click somewhere else – Harald Coppoolse Apr 22 '21 at 13:30