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
Solved

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

Posted on 2004-04-09
2
464 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
  • 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

840 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