Solved

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

16 Experts available now in Live!

Get 1:1 Help Now