0

I have a listbox which I am adding information about an event and whether or not the event is active, completed or inactive etc. Each event will be on a separate line of the listbox and I want the 'activity label' to be colour coded (eg. Green for active, blue for completed etc.) but the rest of the text in black. Does anyone know how to do this?

Seryth
  • 133
  • 1
  • 3
  • 10

1 Answers1

0

Basically you need to set the TListBox.Style to lbOwnerDrawFixed. Create an evnet TListBox.DrawItem. In this event you can do something like this:

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
begin
  with (Control as TListBox).Canvas do
  begin
    FillRect(Rect);
    // Here you can change your Text output to your liking
    Font.Color := clRed;
    TextOut(Rect.Left, Rect.Top, (Control as TListBox).Items[Index]);
  end;
end;
iamjoosy
  • 3,299
  • 20
  • 30
  • As useful as I imagine this is, I'm not too experienced and am a little confused about implementing this. Sorry for my ignorance :/ – Seryth Feb 20 '14 at 11:58
  • @user3327810 If your decscription of how you want the items coded would be clearer, then we could help you with more precise answers. In your case, an image of what you want to achieve could also help, along with the respective data structures that hold the information (active, completed etc) – iamjoosy Feb 20 '14 at 13:27