Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to create a TListBox with coloured entries Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
27-Aug-02
Category
VCL-General
Language
Delphi 2.x
Views
77
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I add protocol messages into a listbox, simple lines of text like "success" and 
"failed". Now I want to have a different background color for every item. For 
example the "failed" ones in red and the "successed" in green. How to achieve this?

Answer:

Put a TListBox on a form, call it ListBox1, set its style to "lbOwnerDrawFixed" and 
implement the following event:
1   
2   procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
3     Rect: TRect; State: TOwnerDrawState);
4   var
5     Flags: Longint;
6   begin
7     with ListBox1 do
8     begin
9       { If the item is not selected, then...}
10      if not (odSelected in State) then
11        with Canvas.Brush do
12        begin
13          { Choose the appropriate color}
14          case Index of
15            0: Color := clBlue;
16            1: Color := clRed;
17            2: Color := clGreen;
18          end;
19        end;
20      { Draw the colored rectangle.}
21      ListBox1.Canvas.FillRect(Rect);
22      if Index < Items.Count then
23      begin
24        Flags := DrawTextBiDiModeFlags(DT_SINGLELINE or DT_VCENTER or DT_NOPREFIX);
25        if not UseRightToLeftAlignment then
26          Inc(Rect.Left, 2)
27        else
28          Dec(Rect.Right, 2);
29        DrawText(Canvas.Handle, PChar(Items[Index]), Length(Items[Index]), Rect, 
30  Flags);
31      end;
32    end;
33  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC