Solved

How to move items in a ListView

Posted on 1997-11-29
2
1,080 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

943 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

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now