Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing font or color of an specific Checklistbox Item?

Tags:

delphi

I'm using Delphi XE-3. I wish to change to color or font of a single item in an checklistbox. Is this possible?

like image 871
Michiel T Avatar asked Nov 28 '12 13:11

Michiel T


1 Answers

You will need to use owner drawing on for your check list box. Set the Style property of your check list box to lbOwnerDrawFixed and write the handler for the OnDrawItem event. In this event handler you can use something like this:

procedure TForm1.CheckListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  Flags: Longint;
begin
  with (Control as TCheckListBox) do
  begin
    // modifying the Canvas.Brush.Color here will adjust the item color
    case Index of
      0: Canvas.Brush.Color := $00F9F9F9;
      1: Canvas.Brush.Color := $00EFEFEF;
      2: Canvas.Brush.Color := $00E5E5E5;
    end;
    Canvas.FillRect(Rect);
    // modifying the Canvas.Font.Color here will adjust the item font color
    case Index of
      0: Canvas.Font.Color := clRed;
      1: Canvas.Font.Color := clGreen;
      2: Canvas.Font.Color := clBlue;
    end;
    Flags := DrawTextBiDiModeFlags(DT_SINGLELINE or DT_VCENTER or DT_NOPREFIX);
    if not UseRightToLeftAlignment then
      Inc(Rect.Left, 2)
    else
      Dec(Rect.Right, 2);
    DrawText(Canvas.Handle, Items[Index], Length(Items[Index]), Rect, Flags);
  end;
end;

Here's the result of the above example:

enter image description here

like image 150
TLama Avatar answered Oct 20 '22 08:10

TLama