Solved

How do Color font and Row Background in listview

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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