Author: Tomas Rutkauskas
I have a treeview which I need to be able to drag items from onto a listbox (they
have to be deleted from the treeview when moved, of course). I have been able to do
this between two listboxes, but this one eludes me. Can anyone get me started
please?
Answer:
1 2 procedure TForm1.TreeView1MouseDown(Sender: TObject; Button:
3 TMouseButton; Shift: TShiftState; X, Y: Integer);
4 begin5 if TreeView1.Items.Count = 0 then6 exit;
7 if Button = mbLeft then8 TreeView1.BeginDrag(False); {begin drag}9 end;
10 11 procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y:
12 Integer; State: TDragState; var Accept: Boolean);
13 begin14 Accept := (Sender = TreeView1);
15 end;
16 17 procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
18 var19 dx: integer;
20 Node: TTreeNode;
21 begin22 if Source = TreeView1 then23 begin24 Node := TreeView1.Selected;
25 if Node <> nilthen26 begin27 with TListBox(Sender) do28 begin29 dx := ItemAtPos(Point(X, Y), false);
30 Items.Insert(dx, Node.Text);
31 {or use:32 Items.InsertObject(dx, Node.Text, Pointer(Node.Data)); }33 end;
34 Node.Delete;
35 end;
36 end;
37 end;