Delphi:是否可以有一个包含禁用项目的组合框?

标签 delphi combobox

如何让 TComboBox 中的某些项目被禁用?我需要用户看到这些项目,但无法选择它们。

谢谢!

最佳答案

是的,具体方法如下:

TComboBox拖放到表单上,并将Style设置为csOwnerDrawFixed。然后添加事件处理程序

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
const
  INDENT = 3;
begin
  with TComboBox(Control) do
  begin
    FillRect(Canvas.Handle, Rect, GetStockObject(WHITE_BRUSH));
    inc(Rect.Left, INDENT);
    if boolean(Items.Objects[Index]) then
      SetTextColor(Canvas.Handle, clBlack)
    else
      SetTextColor(Canvas.Handle, clGray);
    DrawText(Canvas.Handle,
      PChar(Items[Index]),
      length(Items[Index]),
      Rect,
      DT_SINGLELINE or DT_LEFT or DT_VCENTER or DT_END_ELLIPSIS)
  end;
end;

procedure TForm1.ComboBox1CloseUp(Sender: TObject);
begin
  with TComboBox(Sender) do
    if (ItemIndex <> -1) and not boolean(Items.Objects[ItemIndex]) then
    begin
      beep;
      Perform(CB_SHOWDROPDOWN, integer(true), 0);
    end;
end;

此外,在表单的界面部分中,在声明表单类之前添加

TComboBox = class(StdCtrls.TComboBox)
protected
  procedure WndProc(var Message: TMessage); override;
end;

并将WndProc实现为

procedure TComboBox.WndProc(var Message: TMessage);

  function NextItemIsDisabled: boolean;
  begin
    result := (ItemIndex < Items.Count - 1) and
      not boolean(Items.Objects[ItemIndex + 1]);
  end;

  procedure SelectNextEnabledItem;
  var
    i: Integer;
  begin
    for i := ItemIndex + 1 to Items.Count - 1 do
      if boolean(Items.Objects[i]) then
      begin
        ItemIndex := i;
        Exit;
      end;
    beep;
  end;

  procedure KillMessages;
  var
    msg: TMsg;
  begin
    while PeekMessage(msg,
      Handle,
      WM_KEYFIRST,
      WM_KEYLAST,
      PM_REMOVE) do;
  end;

  function PrevItemIsDisabled: boolean;
  begin
    result := (ItemIndex > 0) and
      not boolean(Items.Objects[ItemIndex - 1]);
  end;

  procedure SelectPrevEnabledItem;
  var
    i: Integer;
  begin
    for i := ItemIndex - 1 downto 0 do
      if boolean(Items.Objects[i]) then
      begin
        ItemIndex := i;
        Exit;
      end;
    beep;
  end;

begin
  case Message.Msg of
    WM_KEYDOWN:
      case Message.WParam of
        VK_DOWN:
          if NextItemIsDisabled then
          begin
            SelectNextEnabledItem;
            KillMessages;
            Exit;
          end;
        VK_UP:
          if PrevItemIsDisabled then
          begin
            SelectPrevEnabledItem;
            KillMessages;
            Exit;
          end;
      end;
  end;
  inherited;
end;

要测试组合框,请编写,例如

procedure TForm1.FormCreate(Sender: TObject);
begin
  ComboBox1.Items.AddObject('Alpha', TObject(true));
  ComboBox1.Items.AddObject('Beta', TObject(true));
  ComboBox1.Items.AddObject('Gamma', TObject(false));
  ComboBox1.Items.AddObject('Delta', TObject(true));
end;

我想您已经明白了 truefalse 的含义 - 它只是意味着启用

关于Delphi:是否可以有一个包含禁用项目的组合框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5583835/

相关文章:

c# - 自定义搜索组合框

delphi - 在 Delphi 2007 中对 Amazon 进行 HTTPS POST 调用

delphi - 为应用程序启用主题

php - Delphi DEC库(Rijndael)加密

c# - WPF 组合框 : Wrong Item is displayed

c# - 我想在组合框中显示包含我搜索的文本的项目

c# - 获取 ComboBox 绑定(bind)多列列表的选定项

delphi - Delphi 2009 的优秀版本控制软件

delphi - C++ 到 Delphi : variable in FOR loop with assigned declaration

wpf - 在 Josh Smith 的文章之后,在 MVVM 的 ListView 中添加 ComboBox