菜鸟想实现一个功能,窗体上有两个listview控件,怎么能实现两个listview控件的内容可以用鼠标互相拖动(多项)!!??? (75分)

  • 主题发起人 主题发起人 wanglonga
  • 开始时间 开始时间
W

wanglonga

Unregistered / Unconfirmed
GUEST, unregistred user!
菜鸟想实现一个功能,窗体上有两个listview控件,怎么能实现两个listview控件的内容可以用鼠标互相拖动!!???
 
这是在一个中拖动的例子,如果看明白了,两个也是一样的。

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls;

type
TForm1 = class(TForm)
ListView1: TListView;
procedure ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
DragItem, TargetItem:TListItem;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
TargetIndex: integer;
begin
DragItem := ListView1.Selected;
TargetIndex := ListView1.DropTarget.Index;
if TargetIndex>DragItem.Index then
TargetItem:=ListView1.Items.Insert(TargetIndex+1)
else TargetItem:=ListView1.Items.Insert(TargetIndex);
TargetItem.Assign(DragItem);
DragItem.Delete;
end;

procedure TForm1.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if Source=ListView1 then accept :=true;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ListView1.DragMode := dmAutomatic;
ListView1.ViewStyle := vsList;
end;

end.
 
这个代码可以实现在listView1中拖到listView2中,反方向同理。我运行过得,可以实现
void __fastcall TForm1::ListView1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
if(Button==mbLeft)
((TListView*)(Sender))->BeginDrag(False,-1);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ListView2DragOver(TObject *Sender, TObject *Source,
int X, int Y, TDragState State, bool &Accept)
{
if(Source->ClassNameIs("TListView"))
Accept=True;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListView2DragDrop(TObject *Sender, TObject *Source,
int X, int Y)
{
TListItem *dragItem;
if((Sender->ClassNameIs("TListView"))&&(Source->ClassNameIs("TListView")))
dragItem=((TListView*)(Sender))->Items->Add();
dragItem->Caption=((TListView*)(Source))->Selected->Caption;
}
 
菜鸟还是不能实现从listview1多选几项移到listview2里?
 
你可以将listview1的multiSelect设为true
 
把MultiSelect设为True。

for i:= 0 to ListView1.Items.Count-1 do if ListView1.Items.Selected then ...
 
我听取了众位的意见,做了如下修改,怎么不好使啊??????
var
Form1: TForm1;
listitem:tlistitem;
implementation

{$R *.DFM}

procedure TForm1.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
accept:=source=listview2;

end;

procedure TForm1.ListView2DragDrop(Sender, Source: TObject; X, Y: Integer);
var
i:integer;
begin
for i:= 0 to ListView1.Items.Count-1 do
begin
listitem:=listview2.Items.add;
listitem.Caption:=listview1.Selected.Caption;
listview1.Selected.Delete;
end;
end;

procedure TForm1.ListView2DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
accept:=source=listview1;
end;

end.

我已经将listview1的multiSelect设为true,怎么不好使啊??

 
将void __fastcall TForm1::ListView2DragDrop中的代码换成如下的代码,即可搞定
void __fastcall TForm1::ListView2DragDrop(TObject *Sender, TObject *Source,
int X, int Y)
{
TListItem *dragitem;
if((Sender->ClassNameIs("TListView"))&&(Source->ClassNameIs("TListView")))
{ TItemStates selected = TItemStates() << isSelected;
TListItem *myItem = ((TListView*)(Source))->Selected;
while (myItem)
{
dragitem=((TListView*)(Sender))->Items->Add();
dragitem->Caption=myItem->Caption;
myItem = ((TListView*)(Source))->GetNextItem(myItem, sdAll, selected);
}
}
}
 

for i:= 0 to ListView1.Items.Count-1 do
if ListView1.Items.Selected then
begin
listitem:=listview2.Items.add;
listitem.Caption:=listview1.Items.Caption
listview1.Items.Delete;
end;
 
zw84611大侠,我改成这个样子,有错,错误信息是access violation address 4009fb2c in module "vcl50.bpl.read of address 00000004."怎么回事啊?
var
Form1: TForm1;
listitem:tlistitem;
implementation

{$R *.DFM}

procedure TForm1.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
accept:=source=listview2;

end;

procedure TForm1.ListView2DragDrop(Sender, Source: TObject; X, Y: Integer);
var
i:integer;
begin

for i:= 0 to ListView1.Items.Count-1 do
if ListView1.Items.Selected then
begin
listitem:=listview2.Items.add;
listitem.Caption:=listview1.Items.Caption;
listview1.Items.Delete;
end;

end;

procedure TForm1.ListView2DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
accept:=source=listview1;
end;
 
有一个比较笨的办法,不过可以实现,如果其它高手有新见解请指教:
private
TargetItem:TListItem;
dragitem: array[0..100] of TListItem;
var
Form1: TForm1;
implementation
procedure TForm1.FormCreate(Sender: TObject);
begin
ListView1.DragMode := dmAutomatic;
ListView1.ViewStyle := vsList;
ListView2.DragMode := dmAutomatic;
ListView2.ViewStyle := vsList;
end;
procedure TForm1.ListView2DragDrop(Sender, Source: TObject; X, Y: Integer);
var
TargetIndex: integer;
i,count :integer;
hh : string;
begin
count := listview1.SelCount;
DragItem[0] := ListView1.Selected;
TargetItem:=ListView2.Items.Insert(listview2.Items.Count);
TargetItem.Assign(DragItem[0]);
DragItem[0].Delete;
for i:=1 to count-1 do
begin
dragitem:=listview1.GetNextItem(dragitem, sdAll, [isSelected]);
TargetItem:=ListView2.Items.Insert(listview2.Items.Count);
TargetItem.Assign(DragItem);
DragItem.Delete;
end;
end;

 
多人接受答案了。
 
后退
顶部