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


I wish to display a list. I can easily create the list by adding to Items. Optionally the user can add an item to the list. When I tried it, the OnChange event fired as soon as I pressed one key. I tried using the OnExit - this crashed the app. How can the user type in a string and press Enter and get this added to the list and selected?


By unassigning and re-assigning the OnChange event handler for the combobox. Example:

procedure TForm1.Button1Click(Sender: TObject);
  if Trim(Edit1.Text) <> EmptyStr then
    if ComboBox1.Items.IndexOf(Edit1.Text) = -1 then
      ComboBox1.OnChange := nil;
      ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(Edit1.Text);
      ComboBox1.OnChange := ComboBox1Change;

<< Back to main page