Skip to main content

TcxColorComboBoxItems.GetColorByIndex(Integer,TColor) Method

Gets the color represented by the item at the specified index.

Declaration

function GetColorByIndex(AIndex: Integer; ADefaultColor: TColor): TColor;

Parameters

Name Type
AIndex Integer
ADefaultColor TColor

Returns

Type
TColor

Remarks

Use the GetColorByIndex method if you need to determine the color represented by the item at a specified index. This can be used, for instance, if traversing through the items and performing specific operations on items whose color satisfies a predefined condition.

The AIndex parameter specifies the index of the item whose color is to be retrieved. Note that item indexes are zero based. The highest available item index is the Count property value decremented by one. If the specified AIndex parameter value is negative or exceeds the maximum available index, the color specified by the ADefaultColor parameter value is returned instead.

The following sample code traverses all items within a color combo box control and removes items that are shades of red (with red channel equal to 255, while the green and blue channels are identical).

var
  I: Integer;
  AColor: TColor;
// ...
  I := 0;
  while I <> cxColorComboBox1.Properties.Items.Count do
  begin
    AColor := cxColorComboBox1.Properties.Items.GetColorByIndex(I, clBlack);
    if (ColorIsShadeOfRed(AColor)) then
      cxColorComboBox1.Properties.Items.Delete(I)
    else
      Inc(I);
  end;
// ...
function TForm1.ColorIsShadeOfRed(AColor: TColor): Boolean;
var
  R, G, B: Integer;
  RGB: string;
begin
  RGB := IntToHex(AColor, 6);
  B := StrToInt('$' + LeftStr(RGB, 2));
  G := StrToInt('$' + MidStr(RGB, 3, 2));
  R := StrToInt('$' + RightStr(RGB, 2));
  if (R = 255) and (G = B) then
    Result := True
  else Result := False;
end;
See Also