Mega Code Archive

 
Categories / Delphi / Examples
 

Drag - drop example

Have you ever been using WinAmp? If you haven't, I could tell you that it's a widely spread freeware music-mediaplayer, where you can have a tracklist with your favourite songs. The songs in this list could very easy be moved to any position with the use of drag & drop functionality. This example shows how to implement it with Delphi. {----------------------------------------------------------------------------- Unit Name: Unit1 Author: Mats Asplund Description: Drag & drop example with a Listbox as source and destination. -----------------------------------------------------------------------------} unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) ListBox1: TListBox; procedure ListBox1DragOver(Sender, Source: TObject; X, y: Integer; State: TDragState; var Accept: Boolean); procedure ListBox1DragDrop(Sender, Source: TObject; X, y: Integer); procedure ListBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, y: Integer); procedure FormCreate(Sender: TObject); private FromPos, ToPos: TPoint; procedure MoveItem(FromPos, ToPos: Integer); public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); begin ListBox1.DragMode:= dmAutomatic; // Fill the listbox with some items ListBox1.Items.Add('Item 1'); ListBox1.Items.Add('Item 2'); ListBox1.Items.Add('Item 3'); ListBox1.Items.Add('Item 4'); ListBox1.Items.Add('Item 5'); end; procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, y: Integer; State: TDragState; var Accept: Boolean); begin // Accept Listbox as source Accept:= Source is TListBox; end; procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, y: Integer); begin // Put coordinates in ToPos ToPos.X:= X; ToPos.y:= y; // Check that both sender and source is a ListBox if (Sender is TListBox) and (Source is TListBox) then begin // Get ListBox-item indexes and send them to the MoveItem procedure MoveItem(ListBox1.ItemAtPos(FromPos, true), ListBox1.ItemAtPos(ToPos, true)); end; end; procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, y: Integer); begin // Get sourceitem position FromPos.X:= X; FromPos.y:= y; end; procedure TForm1.MoveItem(FromPos, ToPos: Integer); var n: Integer; DummyList: TStringList; begin DummyList:= TStringList.Create; try if (FromPos > -1) and (ToPos > -1) then begin // Copy listbox to DummyList DummyList.Text:= ListBox1.Items.Text; if FromPos > ToPos then begin // If item is dragged downwards rearrange the list this way ListBox1.Items[ToPos]:= ListBox1.Items[FromPos]; for n:= (ToPos + 1) to FromPos do ListBox1.Items[n]:= DummyList[n - 1]; end else begin // If item is dragged upwards rearrange the list this way ListBox1.Items[ToPos]:= ListBox1.Items[FromPos]; for n:= (ToPos - 1) downto FromPos do ListBox1.Items[n]:= DummyList[n + 1]; end; end; finally DummyList.Free; end; end; end.