Posted on 2002-04-28
Last Modified: 2010-04-04
How do i make each line of a TListBox different Colors?

Line1 < Red
Line 2 < Green?

or Line 3 < Blue and so on, is it possible?

- Thanks.

Craig C.
Question by:craig_capel
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 17

Accepted Solution

inthe earned 70 total points
ID: 6975258
set it to lbOwnerDrawFixed and on the DrawItem event:

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
  with TListBox(Control) do begin
    case Index mod 4 of
      0: Canvas.Brush.Color := clRed;
      1: Canvas.Brush.Color := clSilver;
      2: Canvas.Brush.Color := clBlue;
      3: Canvas.Brush.Color := clYellow;
//      etc.. or use some loop
    case Index mod 4 of
      0: Canvas.Font.Color := clWhite;
      1: Canvas.Font.Color := clRed;
      2: Canvas.Font.Color := clWhite;
      3: Canvas.Font.Color := clNavy;
    Canvas.TextRect(Rect, Rect.Left, Rect.Top, Items.Strings[Index]);
LVL 17

Expert Comment

ID: 6975345
btw if you dont want to use specific colors or an aray you can make it random or something, ie:

Drawitem event :

 i,j : integer;
  j := random(6555536);
  with TListBox(Control) do begin
  for i := 0 to TListBox(Control).Items.Count -1 do begin
      Canvas.Brush.Color := j;

Author Comment

ID: 6982867
Thanks - sorry for the long delay - thanks :)

The Index confused me for a while, Index can not be accessed unless you do With Object Do (For Tlistbox)
but i had a hard time just making the 3rd or 4th box change color, because in your routine, you add then indext gets incremented correct? oh well - you get the points thanks :)

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question