Getting the root text of a TreeNodeShape

TeeTree VCL for Borland Delphi and C++ Builder.
Post Reply
rob2xx2
Newbie
Newbie
Posts: 2
Joined: Fri Nov 15, 2002 12:00 am

Getting the root text of a TreeNodeShape

Post by rob2xx2 » Tue Jun 29, 2004 10:04 pm

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.

tom
Advanced
Posts: 211
Joined: Mon Dec 01, 2003 5:00 am
Contact:

Post by tom » Wed Jun 30, 2004 6:49 pm

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.

rob2xx2
Newbie
Newbie
Posts: 2
Joined: Fri Nov 15, 2002 12:00 am

Post by rob2xx2 » Wed Jun 30, 2004 7:55 pm

Thanks for the very prompt and helpful reply!

Regards

Rob

tom
Advanced
Posts: 211
Joined: Mon Dec 01, 2003 5:00 am
Contact:

Post by tom » Mon Jul 05, 2004 10:30 pm

My pleasure

Post Reply