Delphi 如何引用在“中创建的对象”;加上;陈述

Delphi 如何引用在“中创建的对象”;加上;陈述,delphi,with-statement,Delphi,With Statement,我在运行时创建嵌套组件。如何在带有的中指定子组件的父属性 with Tspanel.Create(categorypanel) do begin parent:=categorypanel; // categorypanel, is a declared variable height:=30; visible:=true; button1 := tsbutton.Create(); // Here is my problem! I want the parent to

我在运行时创建嵌套组件。如何在带有的
中指定子组件的
父属性

with Tspanel.Create(categorypanel) do
begin
  parent:=categorypanel;  // categorypanel, is a declared variable
  height:=30;
  visible:=true;

  button1 := tsbutton.Create();
  // Here is my problem! I want the parent to be the
  // panel I've created with the "with tspanel.create(...)"
  button1.Parent := ...
end;

我的目标是不要为每个组件声明变量。

您不能用
with
语句来做您想做的事情。无法命名with语句的主题对象

改用局部变量。例如:

var
  Panel1: TPanel
  Button1: TButton;
....
Panel1 := TPanel.Create(Form1);
Panel1.Parent := Form1;
Button1 := TButton.Create(Panel1);
Button1.Parent := Panel1;

作为一个额外的好处,您可以使用对任何代码都有影响的
语句删除这些

“with”很少值得。麻烦太多了。。。。您将能够调试IDE中的命名对象。“我的目标是不为每个组件声明变量”-为什么不?