Solved

How to move items in a ListView

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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

10 Experts available now in Live!

Get 1:1 Help Now