TListView 里面设置的是 viewstyle:=vsReport;格式,怎么样实现里面的记录可以上下拖动

解决方案 »

  1.   

    1.在窗体的private声明处定义一个变量:TmpListItem:TListItem;2.写以下3个事件:procedure TFrmMain.ListView1DragOver(Sender, Source: TObject; X,
      Y: Integer; State: TDragState; var Accept: Boolean);
    begin
      if (Source is TListView)and(TmpListItem<>nil) then
      begin
        Accept:=(ListView1.GetItemAt(X,Y)<>TmpListItem);
      end;
    end;procedure TFrmMain.ListView1EndDrag(Sender, Target: TObject; X,
      Y: Integer);
    var
      T2:TListItem;
    begin
      T2:=ListView1.GetItemAt(X,Y);
      if (Target is TListView)and(TmpListItem<>nil)and(T2<>TmpListItem) then
      begin
        if T2.Index<TmpListItem.Index then
          T2:=ListView1.Items.Insert(T2.Index)
        else T2:=ListView1.Items.Insert(T2.Index+1);
        T2.Caption:=TmpListItem.Caption;
    //    T2.ImageIndex:=TmpListItem.ImageIndex;
    //    关于这个Item的处理都写在这里
        TmpListItem.Delete;
        TmpListItem:=nil;
        ListView1.Refresh;
      end;
    end;procedure TFrmMain.ListView1MouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    begin
      if not ListView1.Dragging then
      begin
        TmpListItem:=ListView1.GetItemAt(X,Y);
        ListView1.BeginDrag(True);
      end;
    end;
      

  2.   

    首先ListView1的HotTrack属性设为True。
      down: boolean = False;
      index: integer = -1;implementation{$R *.dfm}procedure TForm1.ListView1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      index := ListView1.ItemIndex;
      if index < 0 then Exit;
      down := True;
    end;procedure TForm1.ListView1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    var
      cap: string;
      tmp: TStringList;
    begin
      tmp := TStringList.Create;;
      if down then
      begin
        if (ListView1.ItemIndex >= 0) and (index >= 0) and (ListView1.ItemIndex <> index) then
        begin
          // 交换
          cap := ListView1.Items[index].Caption;
          tmp.Assign(ListView1.Items[index].SubItems);
          ListView1.Items[index].Caption := ListView1.Selected.Caption;
          ListView1.Items[index].SubItems.Assign(ListView1.Selected.SubItems);
          ListView1.Selected.Caption := cap;
          ListView1.Selected.SubItems.Assign(tmp);
        end;
      end;
      tmp.Free;
    end;
      

  3.   

    呵呵
    已经有人说了的_____________________
    http://lysoft.7u7.net