Dear TMS Staff,
I know Drag and Drop is possibile on TAdvPolyList.
I would like to know if is it possible to drag & drop over an item (TCustomItem, TImageItem, and so on..)
If already possible, or deriving my own Item.
Looking into TCustomItem in can't find where to find DragOver and DragDrop events.
Please could you give me a suggestion? thank you
Hi,
Drag & Drop needs to be implemented manually, below is code that inserts or adds an item based on TCustomItem. The item also be a custom class that inherits from TCustomItem.
The code below is drag & drop code based on 2 TAdvPolyList instances, where the item to be dropped is obtained from the DropItem function. But the Source can come from another component as well.
procedure TForm1.AdvPolyList1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
it, itdrop: TCustomItem;
begin
it := (Source as TAdvPolyList).DropItem;
itdrop := AdvPolyList1.List.ItemAtXY(X, Y);
if Assigned(itdrop) then
AdvPolyList1.InsertItem(itdrop.Index, TCustomItemClass(it.ClassType)).Assign(it)
else
AdvPolyList1.AddItem(TCustomItemClass(it.ClassType)).Assign(it);
end;
procedure TForm1.AdvPolyList1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := Source is TAdvPolyList;
end;
Thank you. Very clear answer.
Key is AdvPolyList1.List.ItemAtXY(X, Y) method, which allows to guess which item is under the mouse pointer.
PS (please do not consider "bool accept=false", it has no meaning in the example)
DropItem is reinitialized each time a DragOver event occurs on the TCustomItemsContainer. So while you are dragging an item outside of the list it could be possible that the last X/Y coördinates do not detect an item and therefore setting FDropItem to nil. To make sure you are always dragging an item, it would be better to use the OnMouseDown event and set a FDropItem variable manually via the ItematXY function