Solved

How can I change the order of the Items in a ListView by drag and drop?

Posted on 2004-04-09
2
508 Views
Last Modified: 2010-04-05
Hi,

I'd like to change the order of the Items in a ListView just by drag and drop with the mouse?
Is this possible?


thanks
0
Comment
Question by:Bohne
[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
2 Comments
 
LVL 11

Accepted Solution

by:
shaneholmes earned 125 total points
ID: 10791382
To drag items within a listview there are three things to do,
first set the dragmode in obect inspector to dmAutomatic.

In the OnDragOver event set the drag accept conditions, for example:

Procedure TForm1.ListView1DragOver(Sender, Source : TObject; X, Y :
Integer;
  State : TDragState; Var Accept : Boolean);
Begin
  Accept := (Source Is TListView)
        and (Source = Sender) // only accept from same  listview
End;

The list item manipulation code goes in the OnDragDrop event.
The following code will swap two items if one is dropped on the other
or put a dragged item at the end of the list if dropped on an empty
space.

Procedure TForm1.ListView1DragDrop(Sender, Source : TObject; X, Y :
Integer);
Var
  TargetItem, TempItem : TListItem;
Begin
  With TListView(Sender) Do
    Begin
      TempItem := items.add;
      TempItem.Assign(selected);
      TargetItem := GetItemAt(X, Y);
      If (TargetItem <> Nil) Then
        Begin
          selected.Assign(TargetItem);
          TargetItem.Assign(TempItem);
          TempItem.delete
        End
      Else
        selected.delete;
      Arrange(arAlignLeft)
    End;
End;

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10791454
If you want to do it at runtime by a click of a button (btnUp, btnDown), then the code:

btnUp:

 if  ListView1.ItemIndex <> 0 then
begin
 ListView1.ItemIndex:= ListView1.ItemIndex - 1;
 ListView1.Items[ListView1.ItemIndex].Selected:= True;
 ListView1.Items[ListView1.ItemIndex].Focused:= True;
end;

btnDown:

if  ListView1.ItemIndex <> ListView1.Items.Count - 1  then
begin
 ListView1.ItemIndex:= ListView1.ItemIndex + 1;
 ListView1.Items[ListView1.ItemIndex].Selected:= True;
 ListView1.Items[ListView1.ItemIndex].Focused:= True;
end;


Shane
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

690 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