Link to home
Start Free TrialLog in
Avatar of dr_gonzo
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(Sender, 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;
ASKER CERTIFIED SOLUTION
Avatar of JimBob091197
JimBob091197

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LorenScott
LorenScott

JB's alternate solution seems to only apply when TListView.ViewStyle is either vsIcon or vsSmallIcon.  However, when using the component with a ViewStyle of vsReport, JB's solution has no effect at all.

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(Sender, 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; //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.Index);
  li3.Caption := li2.Caption;
  li3.Data := li2.Data;
  li3.StateIndex := li2.StateIndex;
  li2.Delete;
end;
//----------------------------------//

--Loren--