# count a treeview root items

hey all,
anyone have a function that can count the root items in a treeview?
not the total item count, I dont want to count the children....
LVL 4
###### Who is Participating?

Commented:
try this code

function TotalRootNodes(ATreeView: TTreeview): Integer;
var
I,ANodes : Integer;
begin
ANodes := 0;
for I := 0 to ATreeView.Items.Count-1 do
if ATreeView.Items[I].Parent = nil then
inc(ANodes);
Result := ANodes;
end;

A sample call would be

numItems:= TotalRootNodes(TreeView1);

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20693535.html

you might find something else useful from it

David
0

Commented:
You might be interested in a treeview which is a huge amount better than the delphi default one Virtual treeview from www.delphi-gems.com - it has a rootnodecount property and has many more options... its well worthwhile if you are going to be doing anything serious with a treeview

David

0

Commented:
another possibility (just from head, not tested)

function countChilds(ATree : TTreeView;ANode : TTreeNode) : Integer;
var
CNode : TTreeNode;
begin
result := 0;
if ANode = Nil then
CNode := ATree.Items.GetFirstNode
else
cNode := ANode.GetFirstChild;
if assigned(CNode) then
begin
while assigned(CNode) do
begin
result := result +1;
cNode := cNode.GetNextSibling;
end;
end;
end;

meikl ;-)
0

Commented:
Thanks for the points :)

I beat the mighty kretzschmar  ;-p) he he ;)

David
0

Commented:
:-))
so you was never in danger :-))

btw. i'm not mighty, i'm just a simple coder

meikl ;-)
0

Commented:
lol ;P
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.