unit WinForm2;

interface

uses
  System.Drawing, System.Collections, System.ComponentModel,
  System.Windows.Forms, System.Data;

type
  TWinForm2 = class(System.Windows.Forms.Form)
  {$REGION 'Designer Managed Code'}
  strict private
    /// <summary>
    /// Required designer variable.
    /// </summary>
    Components: System.ComponentModel.Container;
    ListBox1: System.Windows.Forms.ListBox;
    ListBox2: System.Windows.Forms.ListBox;
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    procedure InitializeComponent;
    procedure TWinForm2_Load(sender: System.Object; e: System.EventArgs);
    procedure ListBox1_MouseDown(sender: System.Object; e: System.Windows.Forms.MouseEventArgs);
    procedure ListBox2_DragOver(sender: System.Object; e: System.Windows.Forms.DragEventArgs);
    procedure ListBox2_DragDrop(sender: System.Object; e: System.Windows.Forms.DragEventArgs);
    procedure Button1_Click(sender: System.Object; e: System.EventArgs);
    procedure ListBox1_DragEnter(sender: System.Object; e: System.Windows.Forms.DragEventArgs);
  {$ENDREGION}
  strict protected
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    procedure Dispose(Disposing: Boolean); override;
  private
  sourcelb: ListBox;
    { Private Declarations }
  public
    constructor Create;
   var a: string;
  end;

  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm2))]

implementation

{$AUTOBOX ON}

{$REGION 'Windows Form Designer generated code'}
/// <summary>
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure TWinForm2.InitializeComponent;
begin
  Self.ListBox1 := System.Windows.Forms.ListBox.Create;
  Self.ListBox2 := System.Windows.Forms.ListBox.Create;
  Self.SuspendLayout;
  // 
  // ListBox1
  // 
  Self.ListBox1.AllowDrop := True;
  Self.ListBox1.ItemHeight := 16;
  Self.ListBox1.Location := System.Drawing.Point.Create(10, 9);
  Self.ListBox1.Name := 'ListBox1';
  Self.ListBox1.SelectionMode := System.Windows.Forms.SelectionMode.MultiExtended;
  Self.ListBox1.Size := System.Drawing.Size.Create(259, 212);
  Self.ListBox1.TabIndex := 0;
  Include(Self.ListBox1.MouseDown, Self.ListBox1_MouseDown);
  Include(Self.ListBox1.DragOver, Self.ListBox2_DragOver);
  Include(Self.ListBox1.DragDrop, Self.ListBox2_DragDrop);
  Include(Self.ListBox1.DragEnter, Self.ListBox1_DragEnter);
  // 
  // ListBox2
  // 
  Self.ListBox2.AllowDrop := True;
  Self.ListBox2.ItemHeight := 16;
  Self.ListBox2.Location := System.Drawing.Point.Create(278, 9);
  Self.ListBox2.Name := 'ListBox2';
  Self.ListBox2.Size := System.Drawing.Size.Create(260, 212);
  Self.ListBox2.TabIndex := 1;
  Include(Self.ListBox2.MouseDown, Self.ListBox1_MouseDown);
  Include(Self.ListBox2.DragOver, Self.ListBox2_DragOver);
  Include(Self.ListBox2.DragDrop, Self.ListBox2_DragDrop);
  // 
  // TWinForm2
  // 
  Self.AutoScale := False;
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(6, 15);
  Self.ClientSize := System.Drawing.Size.Create(547, 237);
  Self.Controls.Add(Self.ListBox2);
  Self.Controls.Add(Self.ListBox1);
  Self.Name := 'TWinForm2';
  Self.Text := 'Drag''n''drop (Windows Forms)';
  Include(Self.Load, Self.TWinForm2_Load);
  Self.ResumeLayout(False);
end;
{$ENDREGION}

procedure TWinForm2.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;

constructor TWinForm2.Create;
begin
  inherited Create;
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent;
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;

procedure TWinForm2.ListBox1_DragEnter(sender: System.Object; e: System.Windows.Forms.DragEventArgs);
begin

end;

procedure TWinForm2.Button1_Click(sender: System.Object; e: System.EventArgs);
begin

end;

procedure TWinForm2.ListBox2_DragDrop(sender: System.Object; e: System.Windows.Forms.DragEventArgs);
var index,i: integer;
    SenderLB: ListBox;
begin
SenderLB:=Sender as ListBox;
for i:=0 to SourceLB.Items.Count-1 do
  if SourceLB.GetSelected(i) then
    SenderLB.Items.Add(SourceLB.Items[i]);
for i:=SourceLB.Items.Count-1 downto 0 do
  if SourceLB.GetSelected(i) then
    SourceLB.Items.Remove(SourceLB.Items[i]);
end;

procedure TWinForm2.ListBox2_DragOver(sender: System.Object; e: System.Windows.Forms.DragEventArgs);
begin
if ((e.Data.GetDataPresent(DataFormats.Text))and(e.AllowedEffect=DragDropEffects.Move)) then
e.Effect:=DragDropEffects.move;
end;

procedure TWinForm2.ListBox1_MouseDown(sender: System.Object; e: System.Windows.Forms.MouseEventArgs);
var
  SenderLB: ListBox;
begin
  SenderLB:=Sender as ListBox;
  SourceLB:=Sender as ListBox;
  if (e.Button=System.Windows.Forms.MouseButtons.Left) then
    SenderLB.DoDragDrop(SenderLB.Items[SenderLB.IndexFromPoint(e.X,e.Y)],DragDropEffects.move);
end;

procedure TWinForm2.TWinForm2_Load(sender: System.Object; e: System.EventArgs);
var i: integer;
begin
for i:=0 to 10 do
begin
  ListBox1.Items.Add('Lista 1 : '+i.ToString);
  ListBox2.Items.Add('Lista 2 : '+i.ToString);
end;
end;
end.
