?
Solved

TListBox

Posted on 2002-04-28
3
Medium Priority
?
285 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
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
inthe earned 280 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

840 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