Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with colouring certain subitems in listview 2

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
GB

I'm using a TListView to display some of my entered data.

I've had working for ages this small procedure to colour the rows alternately:
Code:
// OnDrawing ListView1, colour the rows alternately
procedure TMainForm.ListView1CustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
  with ListView1.Canvas.Brush do
  begin
    if Item.Index mod 2 = 0 then Color := $FFFFFF else Color := $F6F4F4;
  end;
end;

Now I'm trying to highlight certain sub-items in red, when they are the first instance entered, as so:

[highlight #FFFFFF]18:20 Harry Fish 20Kg[/highlight]
[highlight #F6F4F4]18:22 Harry Cheese 5Kg[/highlight]
[highlight #FFFFFF]19:01 Fred Fish 3Kg[/highlight]
[highlight #F6F4F4]19:02 John Fish 5Kg[/highlight]
[highlight #FFFFFF]19:07 Fred Milk 2l[/highlight]
[highlight #F6F4F4]19:13 Harry Milk 5l[/highlight]

As the rows of data are entered, or read from file, a line at a time, I'm setting a flag for each entry type which would indicate whether it should be coloured red or not. The flag is set true or false each time to suit.

I'm trying to colour the text with this procedure:
Code:
procedure TMainForm.ListView1AdvancedCustomDrawSubItem(
  Sender: TCustomListView; Item: TListItem; SubItem: Integer;
  State: TCustomDrawState; Stage: TCustomDrawStage; var DefaultDraw: Boolean);
begin
  with ListView1.Canvas.Font do
  begin
    if (SubItem = 1) and (NewNameFlag) then Color := clRed else Color := clBlack;
    if (SubItem = 2) and (NewItemFlag) then Color := clRed else Color := clBlack;
  end;
end;

However, I find this isn't quite working, and I can't get the combination of procedures to work in harmony.

Firstly, the new ListView1AdvancedCustomDrawSubItem procedure has broken the previously working ListView1CustomDrawItem procedure, so now only the caption is coloured alternately.

Also the ListView1AdvancedCustomDrawSubItem procedure is colouring the entire column, as if the flag is always true.

It may be worth saying that I am using ListView1.Items.BeginUpdate and EndUpdate when I load the data from file, though this has never affected the alternate row colouring.

Steve (Delphi 2007 & XP)
 
Again, optimize code to keep activity at a minimum in ListView1AdvancedCustomDrawSubItem. I have seen scroll-bars do weird things and find them hard to control. Any added attempts to control or monitor them seems to just make things worse

RE:
It works the same on my Delphi with or without the line ending semi-colon after Populate!?

The semi-colon (;) has always been optional for the LAST line within a code block. Had we done this
Code:
procedure TMainForm.Button1Click(Sender: TObject);
begin
  Populate;             //now required
  ShoMessage('Done')    //optional here
end;
Here's why: Disk space was once crucial. A .pas space took up less space if we did things like...
Code:
procedure SayHello begin writeln('hello') end;
... or within a proc ...
Code:
  for i:= 1 to 10 do if i = 1 then x[i]:= n else x[i]:= i;
All LINES ended with ";"

Look at function TMainForm.Populate. You'll see other omitted semi-colons. Sometimes it's laziness, other times it reminds me of certain things if I come back to add to the code. Some code shops have rules that state they must be there for consistancy.

You're very welcome and thanks for the star and thank you too Daddy.

Roo
Delphi Rules!
 

Cool, thanks for the info on that, interesting.

I think I'll stick to rigid rules for ending statements, else I'll prob shoot myself in the foot, as in your example.

Today's 'throw-away' society is indeed making itself felt in all things, even programming.
Gone are the days of optimising code to fit into a an allocated size of memory space. Seems all that happens now is throw more hardware at things to get them done.
I wonder how good the operating systems and major apps would be on today's hardware if they were written with efficiency and optimisation in mind rather than bells and whistles and the next launch date...

Still, at least these days we can buy RAM for a tiny fraction of the price of when it came in K's and low Meg sticks!


Steve (Delphi 2007 & XP)
 
Totally agree. I've wondered how fast w3.11 would run on a modern machine, if you could get drivers for it.

DB console apps are a good example in how they really fly w/o all that gui bloat.

I don't think I'll ever be as good at Delphi as I was at BP7 (dos). I kinda miss it.

BTW: I'm currently converting a recent app from StringGrid inteface to ListView. So far, it's not an easy task. The build structure is more different than expected. Already I'm thinking start over from scratch would be much less painful. So, thanks for that. (just kidding!)

Roo
Delphi Rules!
 
I did not do so with TListView, but can recommend these free TVirtualTreeView/TVirtualDrawTree components. Although it took me some time to figure out how to work with the virtual paradighm, I've finally been able to write my custom drawing procedures. The component can work as a tree or as a regular list view and allows you to control almost every element such as column, item, etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top