Советы по Delphi

         

Drag & Drop между двумя компонентами ListBox


Вот пересмотренный OnDragDrop, использующий source (источник) и sender (передатчик) вместо DstList и SrcList. Теперь, если вы установили SrcList и DstList для использования тех же методов OnDragOver и OnDragDrop и создали обработчик события OnDragDrop, то для операции Drag and Drop вы можете использовать оба решения.

procedure TDualListDlg.DstListDragDrop(Sender, Source: TObject; X,
Y: Integer);var
droppedOnIndex: integer;anItem: integer;numberOfItems: integer;begin
if
(Sender is TListbox) and (Source is TListBox) then
begin

droppedOnIndex := TListBox(Sender).ItemAtPos(Point(X,Y),false);numberOfItems := TListBox(Source).SelCount;anItem := 0;
while numberOfItems > 0 dobeginif TListBox(Source).Selected[anItem] = true thenbeginTListBox(Sender).Items.Insert(droppedOnIndex,TListBox(Source).Items[anItem]);TListBox(Source).Items.Delete(anItem);TListBox(Source).Update;TListBox(Sender).Update;numberOfItems := numberOfItems - 1;endelseanItem := anItem + 1;end;end;
end;

Для того, чтобы предотвратить операцию Drag and Drop с одним и тем же компонентом, используйте следующий код в обработчике события OnDragOver:

if (Sender is TListBox) and (Source is TListBox) then
beginif
TListBox(Sender).Name = TListBox(Source).Name thenAccept := FalseelseAccept := true;end

[000445]



Содержание раздела