Author: Tomas Rutkauskas
Say I have a TOutline with these nodes:
Parent1
Node1
Node2
Node3
Parent2
Parent3
And I need to change the order of the nodes. How can I for example step Node2 down
one step at a time and disable any movement when there are no more nodes to move
past at that level (just after Node3). I need to restrict the stepping inside a
group/ level and that it don't move to another parent.
Answer:
Look in the help file for the TTreeNode methods GetNextSibling, GetPrevSibling and
MoveTo. Say you have a form with a tree view and two buttons, labelled up and down.
The code for the onclick events of the up button would look something like this:
1 procedure UpOnClick
2 var3 PrevSibling: TTreeNode;
4 begin5 {If no node is selected, exit the procedure}6 if MyTreeView.Selected = nilthen7 Exit;
8 {If the node the user is trying to move is not a child node, exit the procedure}9 if MyTreeView.Selected.Level <> 1 then10 Exit;
11 with MyTreeView.Selected do12 begin13 PrevSibling := GetPrevSibling;
14 if PrevSibling <> nilthen15 MoveTo(PrevSibling, naInsert);
16 end;
17 end;
18 19 procedure DownOnClick
20 var21 NextSibling: TTreeNode;
22 begin23 {If no node is selected, exit the procedure}24 if MyTreeView.Selected = nilthen25 Exit;
26 {If the node the user is trying to move is not a child node, exit the procedure}27 if MyTreeView.Selected.Level <> 1 then28 Exit;
29 with MyTreeView.Selected do30 begin31 NextSibling := GetNextSibling;
32 if NextSibling <> nilthen33 NextSibling.MoveTo(MyTreeView.Selected, naInsert);
34 end;
35 end;