Mirror

How to change the node order of a TTreeNode (Views: 100)


Problem/Question/Abstract:

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:

procedure UpOnClick
var
  PrevSibling: TTreeNode;
begin
  {If no node is selected, exit the procedure}
  if MyTreeView.Selected = nil then
    Exit;
  {If the node the user is trying to move is not a child node, exit the procedure}
  if MyTreeView.Selected.Level <> 1 then
    Exit;
  with MyTreeView.Selected do
  begin
    PrevSibling := GetPrevSibling;
    if PrevSibling <> nil then
      MoveTo(PrevSibling, naInsert);
  end;
end;

procedure DownOnClick
var
  NextSibling: TTreeNode;
begin
  {If no node is selected, exit the procedure}
  if MyTreeView.Selected = nil then
    Exit;
  {If the node the user is trying to move is not a child node, exit the procedure}
  if MyTreeView.Selected.Level <> 1 then
    Exit;
  with MyTreeView.Selected do
  begin
    NextSibling := GetNextSibling;
    if NextSibling <> nil then
      NextSibling.MoveTo(MyTreeView.Selected, naInsert);
  end;
end;

<< Back to main page