Delphi帮助文件中的一段代码,为什么运行会出错?

prometheusphinx 2001-12-13 04:29:34
function ComponentToString(Component: TComponent): string;
var
BinStream:TMemoryStream;
StrStream: TStringStream;
s: string;
begin
BinStream := TMemoryStream.Create;
try
StrStream := TStringStream.Create(s);
try
BinStream.WriteComponent(Component);
BinStream.Seek(0, soFromBeginning);
ObjectBinaryToText(BinStream, StrStream);
StrStream.Seek(0, soFromBeginning);
Result:= StrStream.DataString;
finally
StrStream.Free;
end;
finally
BinStream.Free
end;
end;

function StringToComponent(Value: string): TComponent;
var
StrStream:TStringStream;
BinStream: TMemoryStream;
begin
StrStream := TStringStream.Create(Value);
try
BinStream := TMemoryStream.Create;
try
ObjectTextToBinary(StrStream, BinStream);
BinStream.Seek(0, soFromBeginning);
Result := BinStream.ReadComponent(nil);

finally
BinStream.Free;
end;
finally
StrStream.Free;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
comp: TComponent;
begin
s := ComponentToString(Self);
comp := StringToComponent(s);
end;
...全文
120 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
cobi 2001-12-13
  • 打赏
  • 举报
回复
我觉得程序代码没有问题,问题是怎么去调用它。
通过单步调试,我发现,
s := ComponentToString(Self);
comp := StringToComponent(s);
中,s取得的是整个窗体中所有控件的定义信息,象一个dfm文件那样。
至于后面一步就是希望通过这个信息去创建同类型的窗体。但是至于为什么出错,我也不知道。但是对button1这样的控件,我的出错信息是property not found;而查找程序
ObjectTextToBinary(StrStream, BinStream);这一句,如果硬是写到memo中,会发现只有form1的定义,而其他的就没有了,不解
prometheusphinx 2001-12-13
  • 打赏
  • 举报
回复
我已经解决了,用RegisterClasses
GradeUtopia 2001-12-13
  • 打赏
  • 举报
回复
改天。
prometheusphinx 2001-12-13
  • 打赏
  • 举报
回复
当然是叫Form1,当然都放了。
kevin_gao 2001-12-13
  • 打赏
  • 举报
回复
不会吧?
你的Form是叫Form1吗?
你放了Button在form上了吗?
你放了Panel在Form上了吗?

如果都没有,那我不知道该说什么了!
prometheusphinx 2001-12-13
  • 打赏
  • 举报
回复
出错信息是:Class TForm1 not Found.
或者Class TButton not Found,Class TPanel not Found等等,这要看传了一个什么Component给ComponentToString。
prometheusphinx 2001-12-13
  • 打赏
  • 举报
回复
怎么注册,为什么要注册,
例如ComponentToString(Button1),它又会出找不到类TButton的错,
难道TButton也要注册吗?
cobi 2001-12-13
  • 打赏
  • 举报
回复
你的出错信息是什么啊
prometheusphinx 2001-12-13
  • 打赏
  • 举报
回复
在Result := BinStream.ReadComponent(nil);一句出错,说是某某类找不到;
看一下ObjectTextToBinary或ObjectBinaryToText的帮助就能找到上述代码。
王集鹄 2001-12-13
  • 打赏
  • 举报
回复
你的类要注册
wanwangzhiwang 2001-12-13
  • 打赏
  • 举报
回复
可能是没引用类
长弓落日 2001-12-13
  • 打赏
  • 举报
回复
在什么地方提示错误?
代码是来自哪里?

5,391

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧