ListView间传数据
private
v:Variant;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//保存
procedure TForm1.Button1Click(Sender: TObject);
var
MaxR:Integer;
MaxC:Integer;
r:integer;
c:integer;
begin
MaxR:=ListView1.Items.Count;
MaxC:=ListView1.Columns.Count;
v:=vararraycreate([0,MaxR,0,MaxC-1],varVariant);
//保存標題
for c:=0 to ListView1.Columns.Count-1do
begin
v[0,c]:=ListView1.Columns.Items[c].Caption;
end;
//保存數據
for r:=0 to ListView1.Items.Count-1do
begin
v[r+1,0]:=ListView1.Items[r].Caption;
for c:=0 to ListView1.Items[r].SubItems.count-1do
begin
v[r+1,c+1]:=ListView1.Items[r].SubItems.Strings[c];
end;
end;
end;
//恢復
procedure TForm1.Button2Click(Sender: TObject);
var
MaxR:Integer;
MaxC:Integer;
r:integer;
c:integer;
s:string;
begin
if not VarIsArray(v) then
Exit;
//恢復標題
r:=VarArrayLowBound(v,1);
for c:=VarArrayLowBound(v,2) to VarArrayHighBound(v,2)do
with ListView2.Columns.Adddo
Caption:=v[r,c];
//恢復數據
for r:=VarArrayLowBound(v,1)+1 to VarArrayHighBound(v,1)do
begin
with ListView2.Items.Adddo
begin
caption:=v[r,VarArrayLowBound(v,2)];
for c:=VarArrayLowBound(v,2)+1 to VarArrayHighBound(v,2)do
SubItems.Add(v[r,c]);
end;
end;
end;