动态建立组件,第一次loadfromfile成功,第二次loadfromfile出错,为什么!谢谢!(50分)

  • 主题发起人 主题发起人 sweidong
  • 开始时间 开始时间
S

sweidong

Unregistered / Unconfirmed
GUEST, unregistred user!
动态建立组件,第一次loadfromfile成功,第二次loadfromfile出错,为什么!谢谢!
procedure TForm1.Button1Click(Sender: TObject);
var
myexcell:Tmygrid;
begin
myexcell:=Texcell.Create(self);
self.InsertControl(myexcell);
myexcell.LoadFromFile('c:/tre1.bb');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
myexcell:Tmygrid;
begin
myexcell:=Texcell.Create(self);
self.InsertControl(myexcell);
myexcell.LoadFromFile('c:/tre2.bb');
end;
 
老兄,你的myexcell的name属性设了没有,是不是用两个缺省的名字有冲突了?
 
可是我分别给他们name值,为何还不行!
 
不要直接用 InsertControl,这样试一试:

procedure TForm1.Button1Click(Sender: TObject);
var
myexcell:Tmygrid;
begin
myexcell:=Texcell.Create(self);
myexcell.Parent := Self;
myexcell.LoadFromFile('c:/tre1.bb');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
myexcell:Tmygrid;
begin
myexcell:=Texcell.Create(self);
myexcell.Parent := Self;
myexcell.LoadFromFile('c:/tre2.bb');
end;
 
放出该控件loadfromfile,savetofile方法,问题是不是出在这里?出错信息“invalid stream format”
procedure Tmygrid.LoadFromFile(FileName: string);
var
lz: Tlzrw1;//第三方压缩流控件
inms: TMemoryStream;
outms: TMemoryStream;
begin
lz := TLzrw1.Create(nil);
lz.CompressMode := Auto;
lz.UseStream := True;
inms := TMemoryStream.Create;
outms := TMemoryStream.Create;
try
if FileExists(FileName) then
begin
inms.LoadFromFile(FileName);
inms.Position := 0;
lz.InputStream := inms;
lz.OutputStream := outms;
try
lz.DeCompress;
outms.Position := 0;
outms.ReadComponentRes(Self);
except
ReadComponentResFile(FileName, Self);
end;
InvalidateExCell;
end;
finally
outms.Free;
inms.Free;
lz.Free;
end;
end;



procedure Tmygrid.SaveToFile(FileName: string);
var
lz: Tlzrw1;
inms: TMemoryStream;
outms: TMemoryStream;
begin
lz := TLzrw1.Create(nil);
lz.CompressMode := Auto;
lz.UseStream := True;
inms := TMemoryStream.Create;
outms := TMemoryStream.Create;
try
if FileName <> '' then
begin
//WriteComponentResFile(FileName, Self);
inms.WriteComponentRes(Self.ClassName, Self);
inms.Position := 0;
lz.InputStream := inms;
lz.OutputStream := outms;
lz.Compress;
outms.SaveToFile(FileName);
end;
finally
outms.Free;
inms.Free;
lz.Free;
end;
end;
 
loadfromfile后付name值!
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部