把form的keypreview := true procedure Tmain.FormKeyPress(Sender: TObject; var Key: Char); begin if key=13 then Combobox1.DroppedDown:=true;end;
代码“Combobox1.DroppedDown:=true;”只是用来判断菜单是否打开,根本不起作用。 请参考下面方法。注意:第一次回车打开菜单,第二次回车关闭菜单。 处理KeyDown事件: procedure TForm1.ComboBox1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); var lParam:integer; begin with ComboBox1 do if Key=VK_RETURN then begin if ComboBox1.DroppedDown then lParam:=0//关闭菜单 else lParam:=1;//打开菜单 Sendmessage(Handle,CB_SHOWDROPDOWN,lParam,0); Key:=0; end; end; 由于KeyPress事件会导致ComboBox收回,所以要屏蔽回车键: procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char); begin if Key=Char(VK_RETURN) then Key:=#0; end;怎么样,解决了吧?!
Combobox1.DroppedDown:=true;
procedure Tmain.FormKeyPress(Sender: TObject; var Key: Char);
begin
if key=13 then
Combobox1.DroppedDown:=true;end;
请参考下面方法。注意:第一次回车打开菜单,第二次回车关闭菜单。
处理KeyDown事件:
procedure TForm1.ComboBox1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
lParam:integer;
begin
with ComboBox1 do
if Key=VK_RETURN then
begin
if ComboBox1.DroppedDown then
lParam:=0//关闭菜单
else
lParam:=1;//打开菜单
Sendmessage(Handle,CB_SHOWDROPDOWN,lParam,0);
Key:=0;
end;
end;
由于KeyPress事件会导致ComboBox收回,所以要屏蔽回车键:
procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
begin
if Key=Char(VK_RETURN) then
Key:=#0;
end;怎么样,解决了吧?!