Delphi的TListView控件拖放选定行操作

时间:2021-12-05 08:30:32
http://www.tansoo.cn/?p=401

Delphi的TListView控件拖放选定行操作的例子,效果图如下:TListView控件拖动选定行到指定位置

具体实现步骤:

一、新建一个Delphi工程,在窗体上放置一个TListView控件(在Win32面板),设置ListView1的属性:

ViewStyle设置为vsReport
DragMode设置为dmAutomatic
RowSelect设置为True 二、添加ListView1的OnDragOver事件的响应代码: procedure TForm1.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := True;
end; 三、添加ListView1的OnDragDrop事件的响应代码: procedure TForm1.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
aItem, bItem: TListItem;
begin
if(Source is TListView) then
begin
bItem := (Source as TListView).Selected;
aItem:=(Source as TListView).Items.Insert
((Source as TListView).Items.IndexOf((Source as TListView).GetItemAt(X,Y)));
aItem.Assign(bItem);
(Source as TListView).Selected.Delete;
aItem.Selected := True;
end;
end; 最后,编译运行就可以了(本例在Delphi7测试成功)。

如果先select多个,然后再拖动呢?