比如窗体上有两个Listbox,下面将Listbox2中的项目拖到ListBox1中
将两个Listbox的DragMode属性设为dmAutomatic.拖放经过事件
procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  if TListBox(Source)=ListBox2 then
    Accept:=true;
end;//拖放落下事件
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  ListBox1.Items.Add(Listbox2.Items[ListBox2.ItemIndex]);
end;可是执行拖放时拖放经过事件 ListBox1DragOver 却执行了3次,不明.

解决方案 »

  1.   

    //鼠标移动就会触发OnDragOver,不只是3次~~
    //以前写的,ListBox之间和自己拖拽,支持多选的情况,供参考~~procedure TForm1.ListBoxDragDrop(Sender, Source: TObject; X, Y: Integer);
    var
      I, J: Integer;
      S: string;
    begin
      J := TListBox(Sender).ItemAtPos(Point(X, Y), True);
      if Sender = Source then begin
        I := TListBox(Sender).ItemIndex;
        if I < 0 then Exit;
        S := TListBox(Sender).Items[I];
        TListBox(Sender).Items.Delete(I);
        TListBox(Sender).Items.Insert(J, S);
      end else for I := Pred(TListBox(Source).Items.Count) downto 0 do
        if TListBox(Source).Selected[I] then begin
          TListBox(Sender).Items.Insert(J, TListBox(Source).Items[I]);
          if J < 0 then J := TListBox(Sender).Items.Count - 1;
          TListBox(Source).Items.Delete(I);
        end;
    end;procedure TForm1.ListBoxDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    begin
      Accept := Source is TListBox;
    end;
      

  2.   

    zswang(伴水清清)(专家门诊清洁工)的很完整了!