如何将ComboBox中的部分选项设为不可选

解决方案 »

  1.   

    你可以在onchange事件中这样做if combobox.text='sfsdkfsdjkf' then
     begin
      ........
     end
    else
     begin]
       .........
     end;
      

  2.   

    要不就是动态的对combobox赋值
      

  3.   

    if Trim(ComboBox1.text) = 'aa' then
     begin
        ComboBox1.Text := '';
     end;
      

  4.   

    我的问题是几个单位共用一套程序,当某一个单位使用该程序时候,其他单位的选项为灰色,不可以选,如某单位ComboBox1.ItemIndex := 5;,其他单位ComboBox1.ItemIndex := 分别为1,2,3,4; 对应的下拉菜单选项为灰色,不可选。
      

  5.   

    这个你只有在其重画事件中自己处理。不过我一般是在读入数据到combobox时,
    将其他的选项过滤掉。你那种下拉框的用法很少见。
      

  6.   

    将组合框的style设置为csOwnerDrawVariable,然候在其自画事件里进行处理FNoSelectItems保存不可选的字符串
    FNoSelectIndexs保存不可选的字符串在组合框中的索引unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        ComboBox1: TComboBox;
        Edit1: TEdit;
        Button1: TButton;
        procedure FormCreate(Sender: TObject);
        procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
        procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
          Rect: TRect; State: TOwnerDrawState);
        procedure Button1Click(Sender: TObject);
        procedure ComboBox1Select(Sender: TObject);
      private
        FNoSelectItems:TStringList;
        FNoSelectIndexs:TStringList;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      FNoSelectItems:=TStringList.Create;
      FNoSelectIndexs:=TStringList.Create;
    end;procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    begin
      FNoSelectIndexs.Free;
      FNoSelectItems.Free;
    end;procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    begin
      with ComboBox1.Canvas do
        begin
          FillRect(Rect);
          if FNoSelectItems.IndexOf(ComboBox1.Items[index])<>-1 then
            begin
              Font.Color:=clGray;
              FNoSelectIndexs.Add(inttostr(Index));
            end;      TextOut(Rect.Left,Rect.Top,ComboBox1.Items[index]);
        end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      FNoSelectItems.Add(edit1.Text);
    end;procedure TForm1.ComboBox1Select(Sender: TObject);
    begin
      if FNoSelectIndexs.IndexOf(inttostr(ComboBox1.ItemIndex))<>-1 then
        begin
          ComboBox1.ItemIndex:=-1;
        end;
    end;end.