VirtualStringTree, move selected NODES down

LMuadDIb
LMuadDIb used Ask the Experts™
on
I am using a VirtualStringtree and I select a few nodes in the tree and I want to move the selected nodes DOWN. Shift them all down one level and shift them all to the bottom of the tree too.

I have the code for moving/shifting them UP and to the TOP of the tree. They work fine. But moving them down causes a problem because if you shift more than one node down the indexing is messed up or I think all you do is moving the selected nodes within themselves because you cant work backwards in the tree (DownTo), you have to iterate forwards only (I think).

There are no "GetLastSelected" or "GetPreviousSelected", just "GetLast" or "GetPrevious"

The following code is what I have to move the nodes UP and to the TOP... they both work.

Hope someone can help me, thanks

procedure TdmVst.MoveSelectedNodesUp(vst:TVirtualStringTree);
var
  i: integer;
  Node,PrevNode: PVirtualNode;
begin
  if vst.SelectedCount = 0 then Exit;
  Node := vst.GetFirstSelected;
  for i := 0 to vst.SelectedCount - 1 do begin
    PrevNode := vst.GetPrevious(Node,false);
    vst.MoveTo(Node,PrevNode,amInsertBefore,false);
    Node := vst.GetNextSelected(Node,false);
  end;

end;

procedure TdmVst.MoveSelectedNodesTop(vst:TVirtualStringTree);
var
  i: integer;
  Node,PrevNode: PVirtualNode;
begin
  if vst.SelectedCount = 0 then Exit;
  Node := vst.GetFirstSelected;
  for i := 0 to vst.SelectedCount - 1 do begin
    PrevNode := vst.GetFirst(False);
    vst.MoveTo(Node,PrevNode,amInsertBefore,false);
    Node := vst.GetNextSelected(Node,false);
  end;
end;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
The logic is slightly different when you are moving items down in the list. If you are starting from the first item, moving it down will cause the next item to shift up which leads to problems. You need to move things starting from the last one, so each movement not going to affect other items which you are going to move next.
Below is code which I use to move selected items down in the list. VST is the name of my TVirtualStringTree instance.



var
  Node, Next: PVirtualNode;
begin
  with VST do
  begin
    Node := GetLast;
    while Assigned(Node) do
    begin
      if vsSelected in Node.States then
      begin
        Next := GetNext(Node);
        if Assigned(Next) and not (vsSelected in Next.States) then
          MoveTo(Node, Next, amInsertAfter, False);
      end;
      Node := GetPrevious(Node);
    end;
  end;
end;

Open in new window

Author

Commented:
perfect :)
and I was able to modify it slightly to work for moving the selected to the bottom of the tree

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial