如何使TAB键关闭TComboBox而不丢失当前位置? [英] How do I make the TAB key close a TComboBox without losing the current position?

查看:91
本文介绍了如何使TAB键关闭TComboBox而不丢失当前位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上有一个TComboBox。其样式属性设置为csDropDownList。如果我打开下拉菜单并用鼠标或键盘选择一个选项,然后按ENTER键,则在触发KeyPress事件处理程序之前,下拉框将关闭并且ItemIndex属性将更改。如果我按下TAB键,则直到KeyPress事件处理程序触发并且焦点移到控件上并且ItemIndex不会更新之前,下拉菜单才会消失。

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.

如果我想让TAB将ItemIndex更新为下拉列表中当前选择的内容,我将如何实现它?

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

推荐答案

将窗体的KeyPreview属性设置为True。

Set the Form's KeyPreview property to True.

ComboBox OnKeyDown事件:

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.

这篇关于如何使TAB键关闭TComboBox而不丢失当前位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆