2

我在运行时创建嵌套组件。如何Parent在 a 中分配子组件的属性with

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;

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

4

1 回答 1

8

你不能用with声明做你想做的事。无法命名作为 with 语句主题的对象。

请改用局部变量。例如:

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

作为一个额外的好处,您可以删除这些with对任何代码都构成范围限制的语句。

于 2013-06-07T14:06:06.433 回答