VirtualTreeview drag and drop to arrange nodes in a list

I have a list of nodes. I would like to add a drag-and-drop-to-rerange function, but I don't know how to do this.

I tried to use the TVirtualStringTree OnDragDrop event, but I could not figure it out. I looked through the documentation and, unfortunately, there is no minimum code sample for simple node drag-dropping.

Please note that this is just a sibling list. No hieraty. :)

+5
source share
2 answers

If you receive data through GetNodeData, then you can implement this as:

uses
  ActiveX;

Assign drag and drop events to the tree:

OnDragAllowed:

procedure TForm1.vt1DragAllowed(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
  var Allowed: Boolean);
begin
  Allowed := True;
end;

OnDragOver:

procedure TForm1.vt1DragOver(Sender: TBaseVirtualTree; Source: TObject; Shift: TShiftState;
  State: TDragState; Pt: TPoint; Mode: TDropMode; var Effect: Integer; var Accept: Boolean);
begin
  Accept := (Source = Sender);
end;

OnDragDrop:

procedure TForm1.vt1DragDrop(Sender: TBaseVirtualTree; Source: TObject; DataObject: IDataObject;
  Formats: TFormatArray; Shift: TShiftState; Pt: TPoint; var Effect: Integer; Mode: TDropMode);
var
  pSource, pTarget: PVirtualNode;
  attMode: TVTNodeAttachMode;
begin
  pSource := TVirtualStringTree(Source).FocusedNode;
  pTarget := Sender.DropTargetNode;

  case Mode of
    dmNowhere: attMode := amNoWhere;
    dmAbove: attMode := amInsertBefore;
    dmOnNode, dmBelow: attMode := amInsertAfter;
  end;

  Sender.MoveTo(pSource, pTarget, attMode, False);

end;

toAutoDeleteMoveNodes False TreeOptions.AutoOptions.

+11

drag'n'drop:

procedure TForm1.vst(Sender: TBaseVirtualTree;
  Source: TObject; DataObject: IDataObject; Formats: TFormatArray;
  Shift: TShiftState; Pt: TPoint; var Effect: Integer; Mode: TDropMode);
var
  pSource, pTarget: PVirtualNode;
  attMode: TVTNodeAttachMode;
  List: TList<PVirtualNode>;
begin
  pTarget := Sender.DropTargetNode;

  case Sender.GetNodeLevel(pTarget) of
    0:
      case Mode of
        dmNowhere:
          attMode := amNoWhere;
        else
          attMode :=  amAddChildLast;
      end;
    1:
      case Mode of
        dmNowhere:
          attMode := amNoWhere;
        dmAbove:
          attMode := amInsertBefore;
        dmOnNode, dmBelow:
          attMode := amInsertAfter;
      end;

  end;
  List:= TList<PVirtualNode>.create();
  pSource :=  Sender.GetFirstSelected();
  while Assigned(pSource) do
  begin
     List.Add(pSource);
     pSource := Sender.GetNextSelected(pSource);
  end;

  for pSource in List do
   Sender.MoveTo(pSource, pTarget, attMode, False);

 List.Free;
end;
0

All Articles