Solved

How do Color font and Row Background in listview

Posted on 2010-09-04
5
421 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
  • 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

774 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