Solved

Color rows of a Listview

Posted on 2010-09-13
2
263 Views
Last Modified: 2012-06-22
Hi,

I want to color the rows like this:

if odd(item.Index) then
Sender.Brush.Color := clSilver
 else
 Sender.Brush.Color := clWIndow;

But I have allready code in the ListView1CustomDrawItem.
Where should I implement it?

Greetings, Peter Kiers
procedure TMainForm.ListView1CustomDrawItem(Sender: TCustomListView;

  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);

Var

 R:TRect;

 i,NC:integer;

 TotWidth:Integer;

begin

 DefaultDraw:=False;

 With Sender.Canvas do

  begin

   if Item.Selected then

    begin

     Brush.Color := clNavy;

     Font.Color:= clWhite;

    end else

    begin

     Brush.Color := clWIndow;

     Font.Color:= clBlack;

    end;

   FillRect(Item.DisplayRect(drBounds));

   R:=Item.DisplayRect(drLabel);

   TextRect(R,R.Left,R.Top,Item.Caption);

   NC:=Min(Item.SubItems.Count+1,TListView(Sender).Columns.Count);

   if NC<2 Then Exit;

   TotWidth:=Sender.Column[0].Width;

   for i:=1 to NC-1 do

    begin

     TextOut(R.Left+TotWidth,R.Top,Item.SubItems[i-1]);

     TotWidth:=TotWidth+Sender.Column[i].Width;

    end;

  end;

end;

Open in new window

0
Comment
Question by:peterkiers
  • 2
2 Comments
 
LVL 25

Expert Comment

by:epasquier
ID: 33665592

...

   if Item.Selected then

    begin

     Brush.Color := clRed;

     Font.Color:= clWhite;

    end else

    begin

     if (Item.Index And 1)=1         // Add a test for index is Odd here

      Then Brush.Color := clCream

      Else Brush.Color := clWhite;

     Font.Color:= clBlack;

    end;

...

Open in new window

0
 
LVL 25

Accepted Solution

by:
epasquier earned 500 total points
ID: 33665631
Ok, I could have used the Odd() function instead of that binary test for first bit ... Old habits die hard
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

15 Experts available now in Live!

Get 1:1 Help Now