How to Move ListBox Items with the Mouse

The TListBox Delphi component displays a collection of items in a scrollable list. Delphi makes it easy to program dragging and dropping into your applications.

Here's how to allow a user to rearrange (change item's position) the items of a ListBox using drag and drop:

  1. Drop a TListBox (named ListBox1) on a form
  2. Add several strings using the Items property
  3. Set ListBox1's DragMode to dmAutomatic (in Form's OnCreate or using Object Inspector at design-time).
  4. Handle LisBox-es MouseDown, DragOver and DragDrop events
var // form level
   StartingPoint : TPoint;

implementation

...

procedure TForm1.FormCreate(Sender: TObject) ;
begin
   ListBox1.DragMode := dmAutomatic;
end;

procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer) ;
var
   DropPosition, StartPosition: Integer;
   DropPoint: TPoint;
begin
   DropPoint.X := X;
   DropPoint.Y := Y;
   with Source as TListBox do
   begin
     StartPosition := ItemAtPos(StartingPoint,True) ;
     DropPosition := ItemAtPos(DropPoint,True) ;

     Items.Move(StartPosition, DropPosition) ;
   end;
end;

procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean) ;
begin
   Accept := Source = ListBox1;
end;

procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer) ;
begin
   StartingPoint.X := X;
   StartingPoint.Y := Y;
end;
How to Move ListBox Items with the Mouse

转载于:https://www.cnblogs.com/tecsoon/archive/2009/06/04/1496286.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值