Solved

How do Color font and Row Background in listview

Posted on 2010-09-04
5
418 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
Comment Utility
Set RowSelect property of ListView to true
0
 
LVL 5

Author Comment

by:eNarc
Comment Utility
that will color the selected row?
0
 
LVL 25

Expert Comment

by:epasquier
Comment Utility
You will have to code an owner draw event
0
 
LVL 25

Expert Comment

by:epasquier
Comment Utility
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
Comment Utility
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

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

10 Experts available now in Live!

Get 1:1 Help Now