Solved

TListBox

Posted on 2002-04-28
3
270 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 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now