Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delphi, Listview - drag-n-nDrop

Posted on 2004-08-17
5
Medium Priority
?
1,101 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 252 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 248 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
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…
Integration Management Part 2
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

885 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