Solved

Delphi, Listview - drag-n-nDrop

Posted on 2004-08-17
5
1,044 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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 …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 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