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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Treeview > Get the Top Most Parent of a Child? 1

Status
Not open for further replies.

simmo09

Programmer
Apr 22, 2009
82
GB
Hi, normally when i deal with treeviews that have more then one level, i can get the parent of the child by doing something like:

ShowMessage(TreeView1.Selected.Parent.Text);

if say i know it is 2 levels down the tree i would use:

ShowMessage(TreeView1.Selected.Parent.Parent.Text);

which would get the root parents text.

BUT, what if i dont know how many levels the tree has, how would i always get the top most node parent?

Thanks!
 
use a while loop:

eg.

Code:
var
  p : TTreeNode;
begin
  p := TreeView1.Selected.Parent;
  while Assigned(p.Parent) do
    p := p.Parent;

p will then equal the topmost parent node.
 
error in my code, change the initial assign to
Code:
p := TreeView1.Selected;

otherwise p will equal nil if the root node is selected.
 
seems to of done the trick thanks griffyn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top