2
votes

I'm having an issue with the combo box. I have an event handler for OnClick which refreshes data based on what item was selected. The problem is when this scenario occurs:

  1. Drop-down the combo box to list the various options
  2. Type on the keyboard to find a matching item
  3. Combo box changes this selection and calls the OnClick event
  4. My screen refreshes due to this selection / event
  5. Click somewhere outside of the combo box to take the focus away from it
  6. Combo box goes back to the previous selection, even though OnClick was already called
  7. Even though Combo box changed back to prior selection, OnClick isn't called again
  8. After this, Combo Box shows different value than what my data actually represents

So when you open a combo box, and type a few letters on the keyboard to find the item in the drop-down list, OnClick is fired which refreshes my screen. But when you click somewhere outside the combo box (thus taking the focus away from it), the combo box changes back to whatever value was previously selected, instead of what I had typed. And at the same time, the OnClick event isn't fired, so the combo box shows the incorrect value compared to what I had loaded on the screen.

How do I make the combo box stay on the selected item in this scenario of typing the item on the keyboard?

2
Testing DroppedDown and if it is dropped down not refreshing the screen would work?Sertac Akyuz
@SertacAkyuz Sorry, I didn't understand what you asked.Jerry Dodge
In the OnClick event, test for the DroppedDown of ComboBox, if it is false, don't refresh the screen.Sertac Akyuz
Gotcha, I'll try thatJerry Dodge
@Jerry - Not neat but you might need to use 'OnCloseUp' to re-synchronize if item index does not match.Sertac Akyuz

2 Answers

1
votes

In my code, I deal with this using the OnCloseUp event. Well, in fact I'm using a sub-classed combo for my drop-down lists and they override both the Change and CloseUp methods:

procedure TMyDropDownList.Change;
begin
  RespondToChange;
  inherited;
end;

procedure TMyDropDownList.CloseUp;
begin
  RespondToChange;
  inherited;
end;

The RespondToChange method reacts to the new ItemIndex value. If it is expensive to react to every single change whilst the combo is dropped down, then you might consider omitting the call to RespondToChange from the Change method.

0
votes

You could use OnExit to make the entry with the keyboard jive with the Index on the ComboBox; where VarS is assigned OnChange and is the answer you would like to keep:

procedure TForm1.ComboBox1Exit(Sender: TObject);
begin
  { Windows keyboard select bug, force use of selected }
  ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(VarS);
end;

I would call this a bug in the ComboBox design.