Solved

Drag Up or Down Position of a Listview Entry

Posted on 2009-05-14
10
271 Views
Last Modified: 2012-05-07
Hi I'd like to be able to move a listview item up and down the listview. is there anyway of doing this with a mouse or button?

for example, to rearrange the items in an order of choice.
0
Comment
Question by:eNarc
  • 4
  • 3
  • 2
10 Comments
 
LVL 12

Accepted Solution

by:
Hypo earned 300 total points
ID: 24386537
Hi,
I don't know if there is any way to alter the index of a ListItem, at least It doesn't seem to at first glance, and maybe that's because the ListView is tightly connected to the WinAPI. Anyway, If you can handle that the ListItems will be destroyed and then recreated when moved, then you could move the items with drag n drop. See the example below, it shows you how you can move the items by deleting and recreating the items.

regards
Hypo.
unit Unit1;
 

interface
 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, ComCtrls;
 

type

  TForm1 = class(TForm)

    procedure ListViewDragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);

    procedure ListViewDragDrop(Sender, Source: TObject; X, Y: Integer);

    procedure FormCreate(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;
 

var

  Form1: TForm1;
 

implementation
 

{$R *.dfm}
 

procedure TForm1.FormCreate(Sender: TObject);

var i : Integer;

    aListView : TListView;
 

begin

  // ListViewA

  aListView := TListView.Create(Self);

  aListView.Parent := Self;

  aListView.BoundsRect := Rect(10, 10, 200, 300);

  aListView.ViewStyle := vsReport;

  aListView.DragMode := dmAutomatic;

  aListView.OnDragOver := ListViewDragOver;

  aListView.OnDragDrop := ListViewDragDrop;

  aListView.MultiSelect := True;

  for i := 0 to 5 do

    aListView.Items.Add.Caption := 'ListViewA '+IntToStr(Random(100));

  With aListView.Columns.Add do begin

    Width := 100;

    Caption := 'Items';

  end;
 

  // ListViewB

  aListView := TListView.Create(Self);

  aListView.Parent := Self;

  aListView.BoundsRect := Rect(210, 10, 420, 300);

  aListView.ViewStyle := vsReport;

  aListView.DragMode := dmAutomatic;

  aListView.OnDragOver := ListViewDragOver;

  aListView.OnDragDrop := ListViewDragDrop;

  aListView.MultiSelect := True;

  for i := 0 to 5 do

    aListView.Items.Add.Caption := 'ListViewA '+IntToStr(Random(100));

  With aListView.Columns.Add do begin

    Width := 100;

    Caption := 'Items';

  end;

end;
 

procedure TForm1.ListViewDragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);

begin

  Accept := (Sender is TListView) and (Source is TListView);

end;
 

procedure TForm1.ListViewDragDrop(Sender, Source: TObject; X, Y: Integer);

var i : Integer;

    aItem : TListItem;

    aTarget : TListView;

    aSource : TListView;

    aSelected : TList;

begin

  aSelected := TList.Create;

  try

    aTarget := Sender as TListView;

    aSource := Source as TListView;
 

    aTarget.Items.BeginUpdate;

    aSource.Items.BeginUpdate;

    // Extract what items are selected...

    for i := 0 to aSource.Items.Count-1 do

      if aSource.Items[i].Selected then

        aSelected.Add(aSource.Items[i]);

    // Get what position item we will above at...

    aItem := aTarget.GetItemAt(X, Y);
 

    // Move items to new listview...

    if Assigned(aItem) then begin

      for i := 0 to aSelected.Count-1 do

        aTarget.Items.Insert(aItem.Index).Assign(aSelected[i]);

    end else begin

      for i := 0 to aSelected.Count-1 do

        aTarget.Items.Add.Assign(aSelected[i]);

    end;

    // Remove source items...

    for i := 0 to aSelected.Count-1 do

      TObject(aSelected[i]).Free;
 

    aItem := aTarget.GetItemAt(X, Y);

    if Assigned(aItem) then

      aItem.Focused := True;

  finally

    aSource.Items.EndUpdate;

    aTarget.Items.EndUpdate;

    aSelected.Free;

  end;

end;
 

end.

Open in new window

0
 
LVL 1

Assisted Solution

by:KillerCode
KillerCode earned 200 total points
ID: 24388599
maybe this will help



function MoveListViewItem(ListView: TListView; ItemFrom, ItemTo: Word): Boolean;

var

  Source, Target: TListItem;

begin

  listview.Items.BeginUpdate;

  try

    Source := ListView.Items[ItemFrom];

    Target := ListView.Items.Insert(ItemTo);

    Target.Assign(Source);

    Target.Selected := True;

    Source.Free;

    Result := True;

  finally

    ListView.Items.EndUpdate;

  end;

end;
 

//Move Down

 if ListView1.Selected <> nil then

  MoveListViewItem(ListView1, ListViewIP1.Selected.Index, ListView1.Selected.Index + 2);
 

//Move Up

 if ListView1.Selected <> nil then

  MoveListViewItem(ListView1, ListView1.Selected.Index, ListView1.Selected.Index -1);

Open in new window

0
 
LVL 5

Author Comment

by:eNarc
ID: 24388661
wow not only does your code drag it also able to drag it into another listview :D thankyou thankyou thankyou :D

I searched "ListViewDragDrop" on google and found this

http://delphi.about.com/od/vclusing/a/arrangelistview.htm 

and I thought it would be relevant to add this for future :D
0
 
LVL 5

Author Comment

by:eNarc
ID: 24388797
oh no, I need an admin to fix this I made a mistake when finalizing.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Author Comment

by:eNarc
ID: 24389223
how does one use that code KillerCode? were do I put it?
0
 
LVL 1

Expert Comment

by:KillerCode
ID: 24389275
u can add it to 2 Buttons next to the listview, or in a PopUpMenu, ill see what i can do with ur other topic now
0
 
LVL 1

Expert Comment

by:KillerCode
ID: 24389357
or thats another thing??
if i didnt get what u mean explain me more plz
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24389651
Ohh, ok... but you got it to work at least?
/Hypo
0
 
LVL 5

Author Comment

by:eNarc
ID: 24399807
thanks coolleomod :D


Hypo: hell yea it works :D it was awesome your code its does exactly what I ask :D being able to move the items from 1 listview to the other is wow! I totally can use that in other apps too :D thankyou thankyou thankyou

KillerCode: your code works too, and is great, I love functions they do make everything easier, almost everything I've coded has a function makes everything a complete package :D

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to use Hashing sha1 in Delphi 2010 4 206
add combobox item based on numbers 9 125
creating threads in delphi 1 84
PHP preg_replace code convert to Delphi 14 36
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

861 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

30 Experts available now in Live!

Get 1:1 Help Now