Solved

TListBox

Posted on 2002-04-28
3
278 Views
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.
0
Comment
Question by:craig_capel
[X]
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
3 Comments
 
LVL 17

Accepted Solution

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

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  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
    end;
    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;
    end;
    Canvas.TextRect(Rect, Rect.Left, Rect.Top, Items.Strings[Index]);
  end;
0
 
LVL 17

Expert Comment

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

Drawitem event :

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

Author Comment

by:craig_capel
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 :)
0

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

742 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