Page 1 of 1

Getting the root text of a TreeNodeShape

Posted: Tue Jun 29, 2004 10:04 pm
by 5887167
I used the following code to get the root text of a TreeNodeShape:

function GetRootText(Shape: TTreeNodeShape): string;
// Extract the text of the root of the tree that Shape belongs to.
// Recurse back up the tree until there are no parents.
var
Ancestor: TTreeNodeShape;
begin
if Shape.Parents.Count > 0 then
begin
Ancestor := Shape.Parent;
Result := GetRootText(Ancestor);
end
else
Result := Shape.Text[0];
end;

With D5, TeeChart 5pro and TeeTree1.04 it worked fine. With TeeTree v 1.05 Shape.Parents.Count is always zero even when Shape.Parent is not nil.

(I know that in v2 TTreeNodeShape has the 'Root' property - but I'm working in v1!).

Is there a more reliable function to obtain the root text?

TIA.

Posted: Wed Jun 30, 2004 6:49 pm
by Tom
Hi,

Instead of using the count property, make use of the fact that a Roots node parent is always nil.

function GetRootText(Shape: TTreeNodeShape): string;
// Extract the text of the root of the tree that Shape belongs to.
// Recurse back up the tree until there are no parents.
var
Ancestor: TTreeNodeShape;
begin
if (Shape.Parent <> nil) then
begin
Ancestor := Shape.Parent;
Result := GetRootText(Ancestor);
end
else
Result := Shape.Text[0];
end;

Regards,
Tom.

Posted: Wed Jun 30, 2004 7:55 pm
by 5887167
Thanks for the very prompt and helpful reply!

Regards

Rob

Posted: Mon Jul 05, 2004 10:30 pm
by Tom
My pleasure