unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Types;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
ComboBox1: TComboBox;
procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
ARect: TRect; State: TOwnerDrawState);
procedure FormCreate(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
ARect: TRect; State: TOwnerDrawState);
const
clBrown : TColor = $0015375F; // Define custom color Brown
clOrange: TColor = $001B5AF8; // Define custom color Orange
ResistorColors: array[0..9] of TColor = (
clBlack, $0015375F, clRed, $001B5AF8, clYellow, // <---THIS WORKS NO ISSUES!
clGreen, clBlue, clPurple, clGray, clWhite
);
ResistorColors: array[0..9] of TColor = (
clBlack, clBrown, clRed, clOrange, clYellow, // //<---THIS CHOKES. SCREENSHOT ATTACHED
clGreen, clBlue, clPurple, clGray, clWhite
);
begin
with (Control as TComboBox).Canvas do
begin
Brush.Style := bsSolid;
// Set the background color for the item
Brush.Color := ResistorColors[Index];
end;
ComboBox1.Canvas.FillRect(aRect);
Brush.Style := bsClear;
ComboBox1.Canvas.TextOut(aRect.Left + 2, aRect.Top + 2, ComboBox1.Items[Index]);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// Populate the combobox with resistor color band names
ComboBox1.Items.Add('Black - 0');
ComboBox1.Items.Add('Brown - 1');
ComboBox1.Items.Add('Red - 2');
ComboBox1.Items.Add('Orange - 3');
ComboBox1.Items.Add('Yellow - 4');
ComboBox1.Items.Add('Green - 5');
ComboBox1.Items.Add('Blue - 6');
ComboBox1.Items.Add('Violet - 7');
ComboBox1.Items.Add('Gray - 8');
ComboBox1.Items.Add('White - 9');
// Set the combobox style to owner-draw
ComboBox1.Style := csOwnerDrawFixed;
end;
end.