[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2004-04-09
2
Medium Priority
?
589 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 500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

650 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