Solved

How do Color font and Row Background in listview

Posted on 2010-09-04
5
425 Views
Last Modified: 2012-06-21
Hi, I'd like to be able to Color the font and Background of a row in listview when I click on the item, and only that item is colored.

when I select another than one is colored and the rest are not.

how is this possible?
0
Comment
Question by:eNarc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 13

Expert Comment

by:aflarin
ID: 33604426
Set RowSelect property of ListView to true
0
 
LVL 5

Author Comment

by:eNarc
ID: 33606367
that will color the selected row?
0
 
LVL 25

Expert Comment

by:epasquier
ID: 33607721
You will have to code an owner draw event
0
 
LVL 25

Expert Comment

by:epasquier
ID: 33607744
I mean, if you want to change the colors and not use the default ones, otherwise, aflarin is right and RowSelect property is enough.
0
 
LVL 25

Accepted Solution

by:
epasquier earned 500 total points
ID: 33674664
Here is a sample with OwnerDraw property set to True and a CustomDrawItem event. From this you can customize whatever you wish about the items and subitems drawing (colors, positions...)
procedure TForm1.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 := clRed;
     Font.Color:= clWhite;
    end else
    begin
     if (Item.Index And 1)=1
      Then Brush.Color := clGray
      Else Brush.Color := clWhite;
     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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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