Solved

ListView items and colour problem.

Posted on 2003-11-10
8
959 Views
Last Modified: 2010-04-05
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.
0
Comment
Question by:Jaymol
  • 4
  • 4
8 Comments
 
LVL 17

Expert Comment

by:geobul
ID: 9716847
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
0
 
LVL 6

Author Comment

by:Jaymol
ID: 9717065
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.
0
 
LVL 6

Author Comment

by:Jaymol
ID: 9717089
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.
0
 
LVL 17

Expert Comment

by:geobul
ID: 9718594
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
0
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.

 
LVL 6

Author Comment

by:Jaymol
ID: 9720723
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.
0
 
LVL 17

Accepted Solution

by:
geobul earned 500 total points
ID: 9721008
Hi John,

I knew it could be done. Here it is (OnDrawItem only):

Set OwnerDraw to true again :-)

procedure TForm1.ListView1DrawItem(Sender: TCustomListView;
  Item: TListItem; Rect: TRect; State: TOwnerDrawState);
var
  i: integer;
  X: integer;
begin
  if odSelected in State then begin // selected text
    Sender.Canvas.Font.Color := clRed;
    Sender.Canvas.Brush.Color := clYellow;
  end else begin // regular text - not selected
    Sender.Canvas.Font.Color := clBlack;
    Sender.Canvas.Brush.Color := clWhite;
  end;

  // bold font
  if Item.Caption = 'bbb' then begin // your condition here
    Sender.Canvas.Font.Style := [fsBold];
  end else begin
    Sender.Canvas.Font.Style := [];
  end;

  // draw item and subitems
  Sender.Canvas.FillRect(Rect);
  Sender.Canvas.TextOut(Rect.Left + 1, Rect.Top + 1,Item.Caption);
  if Item.SubItems.Count > 0 then begin
    X := 0;
    for i := 0 to Item.SubItems.Count - 1 do begin
      X := X + (Sender as TListView).Columns.Items[i].Width;
      Sender.Canvas.TextOut(Rect.Left + X, Rect.Top + 1,Item.SubItems[i]);
    end;
  end;
end;

Regards, Geo
0
 
LVL 6

Author Comment

by:Jaymol
ID: 9722202
Spot on Geo!  Thanks a lot for your help.

John.
0
 
LVL 17

Expert Comment

by:geobul
ID: 9722262
My pleasure. It was a challenge. Thanks for the grade.
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

Title # Comments Views Activity
Virtualstringtree carsh after invalidate for while 5 86
Delphi cmd execution 6 55
add combobox item based on numbers 9 119
control image tags in a string ? 12 110
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…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
A short film showing how OnPage and Connectwise integration works.

919 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

12 Experts available now in Live!

Get 1:1 Help Now