Move Rows (up-down)
This code gives users the ability to move the items (rows) in vertical direction (up-down).
Example Software: Image to PDF Converter
procedure TForm1.FormCreate(Sender: TObject);
begin
ListView1.DragMode := dmAutomatic;
end;
procedure TForm1.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
If Source = ListView1 then
begin
Accept := True;
end;
end;
// Supports Multiple Items
procedure TForm1.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
currentItem, nextItem, dragItem, dropItem: TListItem;
begin
if (Sender is TListView) and (Source is TListView) then
begin
dropItem := TListView(Sender).GetItemAt(X, Y);
currentItem := TListView(Source).Selected;
while Assigned(currentItem) do
begin
nextItem := TListView(Source).GetNextItem(currentItem,SdAll,[IsSelected]);
if Assigned(dropItem) then
dragItem := TListView(Sender).Items.Insert(dropItem.Index)
else
dragItem := TListView(Sender).Items.Add;
dragItem.Assign(currentItem);
if (Source = Sender) then
currentItem.Free;
currentItem := nextItem;
end;
end;
end;
// Supports Single Item
procedure TForm1.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
MyItem : TListItem;
begin
if Source = ListView1 then
with Source as TListView do
begin
if GetItemAt(X,Y)<>nil then
begin
MyItem:=Items.Insert(GetItemAt(X,Y).Index);
MyItem.Assign(Selected);
Selected.Delete;
end;
end;
end;
Move Columns (left-right)
Example Software: CSV to VCF Converter
procedure TForm1.FormCreate(Sender: TObject);
begin
ListView1.FullDrag := True;
end;
If you don’t want the column names to change. Use this trick:
procedure TForm1.ListView1ColumnDragged(Sender: TObject);
begin
Timer1.Enabled:=True;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled:=False;
ListView1.Columns[0].Caption:='Name';
ListView1.Columns[1].Caption:='Phone';
ListView1.Columns[2].Caption:='Address';
end;
TListBox: https://engineertips.wordpress.com/2020/06/13/delphi-tlistbox-move-by-dragdrop-reorder/
will this work in fmx?
LikeLike