Board index » delphi » Treeviews (again) Please help!!

Treeviews (again) Please help!!

Hi,

Thanks to everyone who sent me personal responses as well as ones to the
lists. I have worked out how to use the TreeView pretty well now...except
for one thing.

Here is an explanation of what I want to do:

I have 5 nodes that are ALWAYS there, and when the form loads it sets all
the images for these 5 nodes. However, the user can create their own nodes
(which are saved - using the SaveToFile method) and all these nodes have the
same image (a folder icon). How do I work out how many other entries there
are and then append the icons to them?

I thought I could use code similar to this, but it seems I cant:

myitems := TreeView.Items.Count;

for x = 5 to myitems do
begin
  ThisNode := TreeView1.Items.Item[myitems];
  ThisNode.ImageIndex := myitems;
  ThisNode.SelectedIndex := myitems;
  myitems := myitems - 1;
end;

The code is a bit rough, but I was looking for something similar.

Regards,
Nick

 

Re:Treeviews (again) Please help!!


Quote
Nick Wiseman wrote in message <6uafre$1d...@news.mel.aone.net.au>...

>I have 5 nodes that are ALWAYS there, and when the form loads it sets all
>the images for these 5 nodes. However, the user can create their own nodes
>(which are saved - using the SaveToFile method) and all these nodes have
the
>same image (a folder icon). How do I work out how many other entries there
>are and then append the icons to them?

>I thought I could use code similar to this, but it seems I cant:

>myitems := TreeView.Items.Count;

>for x = 5 to myitems do
>begin
>  ThisNode := TreeView1.Items.Item[myitems];
>  ThisNode.ImageIndex := myitems;
>  ThisNode.SelectedIndex := myitems;
>  myitems := myitems - 1;
>end;

Firstly, your loop should be counting up to MyItems - 1
since Items is zero-based. Secondly, you are modifying
the value of MyItems within the loop, which is {*word*194}:

for x := 4 to TreeView.Items.Count - 1 do begin
  TreeView.Items[x].ImageIndex := 3; { or whatever}
  TreeView.Items[x].SelectedIndex := 3;
end;

Third, iterating through a TTreeViews Items property
is very slow - it is better to use GetFirstChild, GetNextChild
etc.

Lastly - a simple solution. If all of your user-defined nodes
have the same icon, make this icon the first in the
ImageList that is connected to the TreeView. All nodes
that are added to the tree will default to this icon. After
loading the contents of the tree, all you have to do is
change the icons for your 5 predefined nodes.

HTH

--
Jeremy Collins
Kansai Business Systems

Other Threads