Search code examples
delphitreeviewdelphi-xe6

Treeview Child Delphi


Good day ...

I have a Treeview, which possess several children:

local
----- Son 0
----- Son 1
---------- Recipe 1
-------------------- Value1
----------------------------- Olá1
----------------------------- Olá2
------------------------------------ Select Here
-------------------- Value2
---------- Revenue 2
----- Son 2
----- Son 3

Say, I click the son (SELECT HERE), how do I get the first child of the root node (Son 1) ... I have some code that takes the Root, but I would like to take the child of that root that was clicked :

p: = Form4.TreeView1.Selected.Parent;
           while Assigned (p.Parent) of the Begin
               p: = p.Parent;
           end;

Solution

  • Just use an additional variable to keep track of "previous node", when you reach the root then the previous one is one you need? Ie

    Prev := nil;
    p := Form4.TreeView1.Selected;
    if(p <> nil)then begin
       while Assigned(p.Parent) do begin
          Prev := p;
          p := p.Parent;
       end;
    end;
    // Prev, if assigned, should be the node you need