Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ListView items and colour problem.

Posted on 2003-11-10
8
Medium Priority
?
1,003 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

610 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