如何拖动   listview 的行到想要的位置???????????listview 的 类型设为 vsreport , 现在我想 用鼠标 按住 某一行,进行拖动到想要的位置, 例如:按住 第一行 ,拖动到第三行的位置!!!
请问如何实现???
请给出详细代码!!

解决方案 »

  1.   

    procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
      var
      p: TPoint;  //上下交换位置
    begin
      p.X := X;
      p.Y := Y;
      if ListBox1.ItemIndex <> -1 then begin
        si := ListBox1.ItemAtPos(p, true);
        if si = ListBox1.ItemIndex then
          t := true;
      end;end;
      

  2.   

    晕!!!  我说的是 listview 控件!!!
      

  3.   

    http://community.csdn.net/Expert/topic/4290/4290902.xml?temp=.2082636
      

  4.   

    代码不完成,我说下原理  MOUSEDOWN事件:
     用 ListView1.GetItemAt(X,Y).Caption; 来获得当前鼠标所在行的数据,并存入TListItem变量
     MOUSEMOVE事件
     也是一样的,但存入另一个变量
     MOUSEUP事件
     将2变量的数据进行交换,可能要声明第3个TListItem 变量来进行中转
      

  5.   

    object Form1: TForm1
      Left = 211
      Top = 160
      Width = 870
      Height = 640
      Caption = 'Form1'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      OldCreateOrder = False
      PixelsPerInch = 96
      TextHeight = 13
      object ListView1: TListView
        Left = 208
        Top = 104
        Width = 250
        Height = 150
        Columns = <
          item
            Caption = 'ID'
          end
          item
            Caption = 'Title'
          end>
        DragMode = dmAutomatic
        Items.Data = {
          5F0000000300000000000000FFFFFFFFFFFFFFFF01000000000000000131046D
          616E6500000000FFFFFFFFFFFFFFFF01000000000000000132046E616D650000
          0000FFFFFFFFFFFFFFFF010000000000000001330474796875FFFFFFFFFFFF}
        TabOrder = 0
        ViewStyle = vsReport
        OnDragDrop = ListView1DragDrop
        OnDragOver = ListView1DragOver
      end
    endunit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ComCtrls;type
      TForm1 = class(TForm)
        ListView1: TListView;
        procedure ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
          State: TDragState; var Accept: Boolean);
        procedure ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    begin
      Accept := True ;
    end;procedure TForm1.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
    var
      SourceItem : TListItem;
      TargetItem : TListItem;
      TempItem : TListItem;
    begin
      SourceItem := Listview1.Selected;
      TargetItem := ListView1.GetItemAt(X,Y);
      if TargetItem <> nil then
      begin
        TempItem := Listview1.Items.Insert(TargetItem.Index+1);
        TempItem.Assign(SourceItem);
        SourceItem.Delete;
      end;
    end;end.
      

  6.   

    TANG1981(TANG) 你的代友有很多问题!!! 有时候拖放的位置不正常 和  有时候不能拖放!!!
      

  7.   

    TANG1981(TANG)的代码是可以的。另外,最好指定ListView的RowSelect:=True;
      

  8.   

    我又试了一遍,没问题呀。
    就要ccrun(老妖)所说的,我只是没有把ListView的RowSelect
    设成true,所以不能行选,只能点在第一个列中拖动,但没有你说的那些情况阿