Solved

Delphi, Listview - drag-n-nDrop

Posted on 2004-08-17
5
1,039 Views
Last Modified: 2008-01-09
I'm using Delphi7,

I want to drag and drop items from listview1 to listview2, any sample code?

Thanks in Adv
0
Comment
Question by:Anton007
5 Comments
 
LVL 3

Accepted Solution

by:
Tyrsis earned 63 total points
ID: 11827364
Here you go, a quick sample.  Hope this helps:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, DebugFuncs;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    ListView2: TListView;
    procedure ListViewMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ListViewDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure ListViewDragDrop(Sender, Source: TObject; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ListViewMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
    ListView : TListView;
begin
    // Begin the dragging if this is a list view
    if(Sender is TListView) then
    begin
        ListView := Sender as TListView;
        ListView.BeginDrag(true);
    end;
end;

procedure TForm1.ListViewDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
    // Make sure this is a List View item being dragged
    if(Source is TListView) and (Sender is TListView) then
        Accept := true;
end;

procedure TForm1.ListViewDragDrop(Sender, Source: TObject; X, Y: Integer);
var
    SourceView, DestView : TListView;
    ListItem : TListItem;
begin
    // Don't bother with dragging items to the same list view
    if(Sender = Source) then
        exit;

    // Make sure we're dealing with list views
    if(Sender is TListView) and (Source is TListView) then
    begin
        SourceView := Source as TListView;
        DestView := Sender as TListView;
        ListItem := DestView.Items.Add;
        ListItem.Assign(SourceView.Selected);
        SourceView.Items.Delete(SourceView.Selected.Index);
    end;
end;

end.

Here's the form as text:

object Form1: TForm1
  Left = 368
  Top = 209
  Width = 544
  Height = 376
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ListView1: TListView
    Left = 8
    Top = 8
    Width = 257
    Height = 321
    Columns = <>
    DragMode = dmAutomatic
    Items.Data = {
      3E0000000200000000000000FFFFFFFFFFFFFFFF000000000000000006497465
      6D203100000000FFFFFFFFFFFFFFFF0000000000000000064974656D2032}
    TabOrder = 0
    OnDragDrop = ListViewDragDrop
    OnDragOver = ListViewDragOver
    OnMouseDown = ListViewMouseDown
  end
  object ListView2: TListView
    Left = 272
    Top = 8
    Width = 249
    Height = 321
    Columns = <>
    TabOrder = 1
    OnDragDrop = ListViewDragDrop
    OnDragOver = ListViewDragOver
    OnMouseDown = ListViewMouseDown
  end
end
0
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 62 total points
ID: 11828184
well, have also an older sample:

unit lv_drag_drop_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    ListView2: TListView;
    //this following procedures are assigned to both listviews
    //so that it works vice versa
    procedure ListViewMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure ListViewMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ListViewDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure ListViewDragDrop(Sender, Source: TObject; X, Y: Integer);
  private
    InDrag: Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ListViewMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  If (ssLeft in shift) and
     (not InDrag) and
     ((sender as Tlistview).SelCount > 0) then
  begin
    (sender as Tlistview).BeginDrag(False,10);
    InDrag := True;
  end;

end;

procedure TForm1.ListViewMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  InDrag := False;
end;

procedure TForm1.ListViewDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := (Sender <> Source) and
            (Sender is TListView) and
            (Source is TListView) and
            InDrag;
end;

procedure TForm1.ListViewDragDrop(Sender, Source: TObject; X, Y: Integer);
var
  L,M : TListItem;
  I : Integer;
begin
  if (sender is TListview) and (Source is TListview) then
  begin
    M := TListView(Source).Selected;
    while M <> nil do
    begin
      L := TListView(Sender).Items.Add;
      L.Assign(M);
      //only if wanted to move the items
      //remove this, if wanted to copy
//      M.Delete;
      //end only if wanted to move the items
      M := TListView(Source).GetNextItem(M,sdAll,[isSelected]);
    end;
    InDrag := False;
  end;
end;

end.

meikl ;-)
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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