运行时创建控件数组(40分)

  • 主题发起人 主题发起人 johnpan
  • 开始时间 开始时间
J

johnpan

Unregistered / Unconfirmed
GUEST, unregistred user!
有个问题请教:
在某个程序中,我需要创建一列文字框,然后按照下标填入初值,程序如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
edits: array[0..2] of tedit;
i: integer;
begin
for i:=0 to 2 do
begin
edits:=Tedit.create(Form1);
edits.top:=30+i*25;
edits.left:=30;
edits.width:=130;
edits.height:=20;
edits.text:=inttostr(i+5);
end;
end;

end.

编译通过,但是执行时什么也看不到。为什么?
谢谢
 
加二句:
edits.Parent:=form1;
edits.owner:=form1;

具体的可以去看看李维的<<DELPHI 3从入门到精通>>
 
liwei说的有点错误,应该是:
edits:=Tedit.create(self);
edits.parent := self ;
........

 
柳五公子的方法是对的。在创建TEdit的对象之后需要指定它的载体parent才能显示
出来。
Liwei答案中
edits.parent := form1;
edits.owner := form1; //owner好象是只读属性,不能够直接赋值吧。
 
原来如此,我还以为在CREATE方法的参数里指定
父类呢。那位能说说具体为什么要这样?
 
这两个属性在DELPHI的帮助下有详细的解释,你可以仔细阅读一下。
 
摘录自Delphi Help:
parent:
Inidicates the parent of the control and determines where the control is displayed.
Use the Parent property to determine the parent of this control or to assign a control to be the parent of this control. When creating a new control, always assign a Parent property value for the new control. Usually, this is a form, panel, group box, or some control that is designed to contain another. Changing the parent of a control moves the control onscreen so that it is displayed within the parent.

owner:
Use Owner to find the owner of a component. When one component is owned by another, the memory for the owned component is freed when its owner's memory is freed. This means that when a form is destroyed, all the components on the form are also destroyed.

Don't confuse the Parent property declared in TControl with the Owner property declared in TComponent. The Parent of a control is always a windowed control that visually contains the control. The Owner of a component is the component that was passed as a parameter in the constructor and that controls when the component is freed.
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
684
import
I
I
回复
0
查看
550
import
I
I
回复
0
查看
698
import
I
I
回复
0
查看
553
import
I
I
回复
0
查看
518
import
I
后退
顶部