Перемещение элементов TListBox при помощи мыши
Должны быть выполнены следующие шаги, чтобы получить рабочий код:
Установите свойство DragMode компонента TListBox в dmAutomatic. Затем, Вы должны обеспечить два обработчика событий DragDrop и DragOver для TListBox.
Поместите следующий код в обработчик события DragOver:
procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X,
Y: Integer; State: TDragState; var Accept: Boolean);
var
DropIndex: Integer;
TempStr : String;
begin
with TListBox(Sender) do
begin
DropIndex := ItemAtPos(Point(X,Y), True);
if (DropIndex > -1) and (DropIndex <> ItemIndex) then
begin
TempStr := Items[DropIndex];
Items[DropIndex] := Items[ItemIndex];
Items[ItemIndex] := TempStr;
ItemIndex := DropIndex;
end;
end;
end;
И наконец, поместите этот код в обработчик события DragDrop:
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X,
Y: Integer);
var
DropIndex: Integer;
begin
DropIndex := TListBox(Sender).ItemAtPos(Point(X,Y),False);
with TListBox(Source) do
begin
TListBox(Sender).Items.Insert(DropIndex, Items[ItemIndex]);
Items.Delete(ItemIndex);
end;
end;
Теперь Вы можете перемещать элементы списка при помощи мыши.
Комментарии