?
Solved

How to move items in a ListView

Posted on 1997-11-29
2
Medium Priority
?
1,201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 400 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

764 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