如何防止在组件 TColorBox 中选择特定颜色?

How to prevent the selection of a specific color in a component TColorBox?

有谁知道如何从TColorBox的下拉列表中删除某种颜色?

您可以从 Items collection 中删除预填充的。例如:

procedure TForm31.Button1Click(Sender: TObject);
var
  Index: Integer;
begin
  Index := ColorBox1.Items.IndexOfObject(TObject(clGreen));
  if Index <> -1 then
    ColorBox1.Items.Delete(Index);
end;

您需要从列表中删除以下颜色:

procedure TForm7.FormCreate(Sender: TObject);
var i: Integer;
begin
  i := ColorBox1.Items.IndexOf('clGreen');
  if i <> -1 then
    ColorBox1.Items.Delete(i)
  else
    Showmessage('invalid color');
end;

您的问题标题:

How to prevent the selection of a specific color in a component TColorBox?

所以防止不是删除,你有两个选择:

  • 阻止选择:

    procedure TForm1.FormCreate(Sender: TObject);
     begin
      ColorBox1.ItemIndex := -1;
     end;
    
    procedure TForm1.ColorBox1Change(Sender: TObject);
    begin
    if ColorBox1.Colors[ColorBox1.ItemIndex] = clNavy then //Choose any color
      begin
        ShowMessage('Invalid color');
        ColorBox1.ItemIndex := -1;
      end;
    end;
    
  • 如果您需要删除 Color 那么您有两个答案可以做到这一点。