//这里啦
procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
if source=listbox1 then
  accept:=true;end;procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
 tempstr:string;
 P_move:Tpoint;
begin
 {通过坐标获得ITEM}
 P_move.x:=x;
 p_move.y:=y;
 if (listbox1.itemindex<>-1) and (listbox1.ItemAtPos(P_move,true)<>-1) then
 begin   //互换ITEM
  tempstr:=listbox1.Items.Strings[listbox1.itemindex];
  listbox1.Items.Strings[listbox1.itemindex]:=listbox1.items.Strings[listbox1.ItemAtPos(P_move,true)];
  listbox1.items.Strings[listbox1.ItemAtPos(P_move,true)]:=tempstr;
 end;
end;

解决方案 »

  1.   

    给你又抄了一段,别人写的:)和你一起学习,上面的例子没有拖动过程,下面的有
    注意两个例子都要设置ListBox1.DragMode:=dmAutomatic;
      ....
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      SavedIndex:Integer=0;
    implementation{$R *.DFM}procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
     var Arect:TRect;
      Aitem:integer;
    begin
      Accept:=True;
      Aitem:=TListBox(Sender).ItemAtPos(point(x,y),TRUE);
      Arect:=TListBox(Sender).ItemRect(Aitem);
      if Aitem<>SavedIndex then begin
        TListBox(Sender).Canvas.Brush.Color:=TListBox(Sender).Color;
        TListBox(Sender).Canvas.FrameRect(TListBox(Sender).ItemRect(SavedIndex));
        SavedIndex:=Aitem;
      end;
      TListBox(Sender).Canvas.Brush.Color:=clInactiveBorder;
      TListBox(Sender).Canvas.FrameRect(TListBox(Sender).ItemRect(Aitem));
    end;
    procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
    begin
      ListBox1.Items.Exchange(TListBox(Sender).ItemIndex,TListBox(Sender).ItemAtPos(Point(x,y),True));
    end;procedure TForm1.ListBox1StartDrag(Sender: TObject;
      var DragObject: TDragObject);begin
      SavedIndex:=TListBox(Sender).ItemIndex;
    end;
      

  2.   

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    ListBox1.DragMode:=dmAutomatic;
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      

  3.   

    使用sender,source之前一定要先判断
    if Source is TListBox then Accept:=True;
    而且不要用强制转换,delphi提供了一个类的转换操作符 as 
    (Source as TListBox).Items...
    这样你写了一次之后,以后每次要拖拽的时候都只要将代码复制一下就可以了,什么都不用改。
      

  4.   

    建议用TListView,功能多一些。
      

  5.   

    to gaden007(斌) 

      if Aitem<>SavedIndex then begin
    改为
    if (Aitem<>SaveIndex) and (Aitem>=0) then begin
    就ok
      

  6.   

    thedream(梦幻使者),我试过了, 不行的才来问,你试过可以吗?
      

  7.   

    就是那个错误,加了Aitem>=0也不行的
      

  8.   

    procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
    begin
      ListBox1.Items.Exchange(TListBox(Sender).ItemIndex,TListBox(Sender).ItemAtPos(Point(x,y),True));
    end;这里应该也要加入那句
    即if ListBox(Sender).ItemAtPos(Point(x,y),True))>=0 then 
      ListBox1.Items.Exchange(TListBox(Sender).ItemIndex,TListBox(Sender).ItemAtPos(Point(x,y),True));
      

  9.   

    出错的应该是这个Exchange方法