views:

358

answers:

3

I have a TComboBox on a form. Its Style property is set to csDropDownList. If I open the dropdown and select an option with my mouse or keyboard and hit ENTER, the dropdown box closes and the ItemIndex property is changed before the KeyPress event handler is fired. If I hit TAB, the dropdown doesn't disappear until after the KeyPress event handler has fired and focus has moved off the control, and the ItemIndex doesn't get updated; it reverts back to whatever had been selected before I opened the list.

If I want TAB to update ItemIndex to whatever's currently selected in the dropdown list, how would I implement it?

A: 

I believe this is the default behavior, and to change it you might need to subclass the control (or even a class helper), intercept the windows message for the keystroke, then if its a tab send a return to the control and handle the tab yourself.

skamradt
A: 

You should try to trap TAB earlier in the KeyUp event or maybe even earlier in the KeyDown.

François
A: 

Set the Form's KeyPreview property to True.

In the ComboBox OnKeyDown event:

procedure TForm1.ComboBox1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key = VK_TAB) then
  begin
    Key := VK_RETURN;
    Perform(WM_NEXTDLGCTL,0,0);
  end;
end;

This emulates the return key and then moves focus to the next control.

_J_