dr_gonzo
asked on
How to move items in a ListView
I want to be able to drag-drop items within a ListView. But the item that is to be moved, always get's the last position i the listview. My ListView is set to be vsIcon. And all items moved ends up to the right, why?
This is my code:
procedure T_A_frm.lvw1DragDrop(Sende r, Source: TObject; X, Y: Integer);
var
li,li2, li3 : TListItem;
begin
li2 := (Source As TListView).GetNextItem(nil , sdAll, [isSelected]); // original
li := (Source as TListView).DropTarget; //Droped on
li3 := (Source As TListView).Items.Insert(li .Index);
li3.Caption := li2.Caption;
li2.Delete;
end;
This is my code:
procedure T_A_frm.lvw1DragDrop(Sende
var
li,li2, li3 : TListItem;
begin
li2 := (Source As TListView).GetNextItem(nil
li := (Source as TListView).DropTarget; //Droped on
li3 := (Source As TListView).Items.Insert(li
li3.Caption := li2.Caption;
li2.Delete;
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
However, once again using vsReport ViewStyle, I noticed that Gonzo's original solution did not allow for dragging an item all the way to the bottom. Instead, it would be inserted into the row just above the last item. The small modifications to Gonzo's code that I made support this and also include transfer of the State and Data properties, which my app required.
//------------------------
procedure TForm1.lvToursDragDrop(Sen
var
li,li2, li3 : TListItem;
begin
li2 := (Source As TListView).GetNextItem(nil
li := (Source As TListView).DropTarget; //Dropped on
if (Source As TListView).GetItemAt(X,Y) = nil then
li3 := (Source As TListView).Items.Add
else
li3 := (Source As TListView).Items.Insert(li
li3.Caption := li2.Caption;
li3.Data := li2.Data;
li3.StateIndex := li2.StateIndex;
li2.Delete;
end;
//------------------------
--Loren--