Link to home
Start Free TrialLog in
Avatar of Jaymol
Jaymol

asked on

ListView items and colour problem.

Hi.

I've been trying to figure this out and I'm about to start banging my head against a wall to see if that helps!

I have a function that determines whether or not a string is in an array (simple boolean result).  If the result is true, I want the corresponding item in a TListView to show in normal text.  If it's not, I want it in bold.

I've had it working fine, until I added subitems - subitems don't appear in my list at all.

Anyway, I've done so many modifications trying to get it to work that I don't have any example code and just require a good example of how to do this.

I also need to change the selected colour for the listview as well.

Thanks in advance,

John.
Avatar of geobul
geobul

Hi,

Set OwnerDraw property to true and write something like (OnCustomDrawItem event handler):

procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
  if cdsSelected in State then begin
    Sender.Canvas.Font.Style := [fsBold];
  end else begin
    if Item.Caption = 'bbb' then begin // your condition here
      Sender.Canvas.Font.Style := [fsBold];
    end else begin
      Sender.Canvas.Font.Style := [];
    end;
  end;
end;

Regards, Geo
Avatar of Jaymol

ASKER

Thanks Geo, but that doesn't work.

Firstly, it doesn't show the subitems, and secondly, I asked how to change the colour of the selected item as well.

Also, could you explain why you'd set OwnerDraw to true if you're using a CustomDrawItem event (these do not require ownerdraw to be true.)

John.
Avatar of Jaymol

ASKER

I've got it working - set the OwnerDraw to false and your proc works okay.

Now I just need to change the highlight colour.  How can I do that?

John.
You are right about OwnerDraw. My mistake. I was misleaded by the help of that property.

You want to change the selected color for the Listview as a whole or different colors for different lines depending on a condition? What is ListView.RowSelect property set to? If it is false, you may try:

  if cdsSelected in State then begin
    Sender.Canvas.Font.Style := [fsBold];
    Sender.Canvas.Font.Color := clRed; // add this line only
  end else begin
    // the same

This way the color of the subitems of the selected item will be to red.

If this doesn't work as you want then we could change the system color settings (Windows color scheme) in OnEnter and restore them back in OnExit  events. Some application events will be involved also. The reason is because I think that ListView uses windows color schemes for selected items.

Regards, Geo
Avatar of Jaymol

ASKER

Hi Geo.

Thanks for that, but it's the highlight colour that I want to change, not the text colour.  I've had this working by using Sender.Canvas.FillRect, but then the text wasn't visible.  So, I did a Sender.Canvas.TextRect, but then I did not have any subitems visible.

I can do both the things I want to do, just not at the same time!

Thanks,

John.
ASKER CERTIFIED SOLUTION
Avatar of geobul
geobul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jaymol

ASKER

Spot on Geo!  Thanks a lot for your help.

John.
My pleasure. It was a challenge. Thanks for the grade.