How to add items to a TComboBox upon an [ENTER] key press (2) (Views: 28)
Problem/Question/Abstract: 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? Answer: By unassigning and re-assigning the OnChange event handler for the combobox. Example: procedure TForm1.Button1Click(Sender: TObject); begin if Trim(Edit1.Text) <> EmptyStr then if ComboBox1.Items.IndexOf(Edit1.Text) = -1 then begin ComboBox1.OnChange := nil; ComboBox1.Items.Add(Edit1.Text); ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(Edit1.Text); ComboBox1.OnChange := ComboBox1Change; end; end; |