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 Display a TListBox with alternating colors. 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
Display a TListBox with alternating colors. 08-Jul-04
Category
VCL-General
Language
Delphi 2.x
Views
307
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
Darley, F. Joe
Reference URL:
			1   
2   unit Unit1;
3   
4   interface
5   
6   uses
7     Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
8     Dialogs, StdCtrls;
9   
10  type
11    TForm1 = class(TForm)
12      Button1: TButton;
13      ListBox1: TListBox;
14      procedure Button1Click(Sender: TObject);
15      procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
16        Rect: TRect; State: TOwnerDrawState);
17    private
18      { Private declarations }
19    public
20      { Public declarations }
21    end;
22  
23  var
24    Form1: TForm1;
25  
26  implementation
27  
28  {$R *.dfm}
29  
30  procedure TForm1.Button1Click(Sender: TObject);
31  begin
32   ListBox1.AddItem('Test',nil);
33   ListBox1.AddItem('Test II',nil);
34   ListBox1.AddItem('Test III',nil);
35  
36  end;
37  
38  procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
39    Rect: TRect; State: TOwnerDrawState);
40  var
41    ListColor: TColor;
42    ListBrush: TBrush;
43  begin { TForm1.ListBox1DrawItem }
44  
45    // create a brush to paint the item's background
46    ListBrush := TBrush.Create;
47  
48    // get a canvas to draw - this is a canvas for the complete listbox!
49    with (Control as TListBox).Canvas do
50    begin
51      // put out lines in alternating colors
52      if (index mod 2)=0 then
53        ListColor := clSilver
54      else
55        ListColor := clBlue;
56  
57      ListBrush.Style := bsSolid;
58      ListBrush.Color := ListColor;
59      Windows.FillRect(Handle, Rect, ListBrush.Handle);
60      Brush.Style := bsClear;
61  
62      // write out the text
63      TextOut(Rect.Left, Rect.Top, (Control as TListBox).Items[index]);
64  
65      ListBrush.Free
66    end; { with (Control as TListBox).Canvas }
67  
68  
69  end;
70  
71  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