Solved

Delphi, Listview - drag-n-nDrop

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 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

16 Experts available now in Live!

Get 1:1 Help Now