Mirror

How to add items to a TComboBox upon an [ENTER] key press (Views: 100)


Problem/Question/Abstract:

I would like my user to be able to enter items into a combobox and add each item upon pressing the enter key. Is there a simple way to do this. I started using csdropdown style. Then I tried using the keydown event with key 13, so that when the user presses enter, the user's entry is added to combobox.items, but so far its not working. I'll keep hacking away at it, but I thought perhaps there is an existing solution to this problem, either starting from a different control, or using a different method in TComboBox.

Answer:

This works for me on a csDropDown combobox (D5.01):

procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then
  begin
    if combobox1.text <> emptystr then
      combobox1.items.add(combobox1.text);
    key := #0;
  end;
end;

<< Back to main page