我在程序中动态往一个combobox的菜单中加入了项目,在combobox中打回车能打开菜单吗?

解决方案 »

  1.   

    if key=13 then
    Combobox1.DroppedDown:=true;
      

  2.   

    将上面的代码放于OnKeyDown事件里
      

  3.   

    把form的keypreview := true
    procedure Tmain.FormKeyPress(Sender: TObject; var Key: Char);
    begin
    if key=13 then
    Combobox1.DroppedDown:=true;end;
      

  4.   

    代码“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;怎么样,解决了吧?!
      

  5.   

    聚焦到Combobox,然后按方向键 下 ,不就可以了吗?按一下,就取下一个,像开始->运行 里面的一样,干嘛要回车键呢?疑惑?!