Delphi编程实用小技巧(三)——经典listbox的拖拽

来源:互联网 发布:阿里云服务器续费优惠 编辑:程序博客网 时间:2024/06/05 02:00

首先将listbox的属性:

DrogMod  设置为dmAutomatic

Multiselect 设置为 true

代码:

procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
i:integer;
str:string;
begin
for i:= tlistbox(source).items.count-1 downto 0 do
 begin
  if tlistbox(source).Selected[i] then
    begin
      with source as tlistbox do
      begin
        str:=items[i];
        items.Delete(i);
      end;
      with sender as tlistbox do
      begin
        items.Insert(itematpos(point(x,y),false),str);
      end;
    end;
  end;
end;

procedure TForm1.ListBox2DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
 accept:=false;
 if source is tlistbox then
    with source as tlistbox do
      if items.Count>0 then
       accept:=true;
end;

0 0