Solved

How to move items in a ListView

Posted on 1997-11-29
2
1,106 Views
Last Modified: 2010-04-04
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;
0
Comment
Question by:dr_gonzo
2 Comments
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 200 total points
ID: 1352356
Hi

In your example you are deleting & inserting, which means that individual list item properties are lost (ImageIndex, Data, etc.).

Instead, why not try the following:  (Experiment with the ListView's IconOptions property's AutoArrange on/off.)

procedure TForm1.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
    li : TListItem;
begin
    li := (Source As TListView).Selected;
    li.SetPosition(Point(X, Y));
end;

JB
0
 

Expert Comment

by:LorenScott
ID: 8560109
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--
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question