Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delphi, Listview - drag-n-nDrop

Posted on 2004-08-17
5
Medium Priority
?
1,072 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

730 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