Andrey Angerchik
Andrey Angerchik

Reputation: 51

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

Does anyone know how to remove a certain color from the drop-down list of TColorBox?

enter image description here

Upvotes: 3

Views: 683

Answers (3)

Ilyes
Ilyes

Reputation: 14928

Your question title:

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

So prevent is not delete , and you have two choice:

  • Prevent selection :

    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;
    
  • If you need to delete the Color then you have two answers to do that.

Upvotes: 3

Victoria
Victoria

Reputation: 7912

The prefilled ones you can delete from the Items collection. For example:

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

Upvotes: 5

RBA
RBA

Reputation: 12584

You need to remove the color in the from the list:

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;

Upvotes: 3

Related Questions