Delphi中的FormCreate问题
我正在使用其他人的代码,并且正在添加一个新表单
所以,我已经创建了该表单,我可以打开它,使用按钮和列表等,但我在 formcreate 上执行操作时遇到问题。
我通过这样做来制作表单:
procedure TModelForm.RepeatOpen(Sender: TObject);
var
DefForm : TForm5;
begin
DefForm := TForm5.Create(Self);
Self.Visible := False;
try
DefForm.ShowModal;
finally
Self.Visible := True;
DefForm.Release;
end;
end;
在我的 TForm5 中,我有一个程序
procedure TForm5.FormCreate(Sender: TObject);
begin
inherited;
RunList := CModelList.Create;
RunList.ReadData;
RunList.FillList(ListBox1.Items);
end;
,但它没有执行任何
我也
procedure TForm5.PopulateListClick(Sender: TObject);
begin
RunList := CModelList.Create;
RunList.ReadData;
RunList.FillList(ListBox1.Items);
end;
分配给按钮的操作,这实际上可以工作并填充我的列表框,
我一直在网上查找它并且似乎没有 OnCreate 函数,有一种方法可以覆盖它,但似乎应该有一种方法来定义首次创建框架时发生的情况
,我使用 FormCreate 的原因是因为这就是我正在使用的代码是正在做,而且似乎有效,
谢谢!
I'm working with someone else's code, and I'm adding a new form
So, I've created the form and I can open it, use the buttons and list, etc, but I'm having a problem doing things on formcreate.
I make the form by doing this:
procedure TModelForm.RepeatOpen(Sender: TObject);
var
DefForm : TForm5;
begin
DefForm := TForm5.Create(Self);
Self.Visible := False;
try
DefForm.ShowModal;
finally
Self.Visible := True;
DefForm.Release;
end;
end;
in my TForm5, I have a procedure
procedure TForm5.FormCreate(Sender: TObject);
begin
inherited;
RunList := CModelList.Create;
RunList.ReadData;
RunList.FillList(ListBox1.Items);
end;
but it doesn't do anything
I also have
procedure TForm5.PopulateListClick(Sender: TObject);
begin
RunList := CModelList.Create;
RunList.ReadData;
RunList.FillList(ListBox1.Items);
end;
which is assigned to a button, and this actually works and populates my ListBox
I've been looking it up online and it seems like there is no OnCreate function, there is a way to override it but it seems like there should be a way to just define what happens when the frame is first created
also, the reason I'm using FormCreate is because that's what the code I'm working with is doing, and it seems to be working
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能忘记将
FormCreate
分配给OnCreate
。就我个人而言,我会通过重写构造函数来实现这一点,从而保持 .dfm 形式不受影响。顺便说一句,我想对您编写的代码进行评论:
您不需要为
DefForm
分配所有者,因为您正在承担清理任务,尽管它通常不会对指定一个所有者。更重要的是,try/finally
尝试做两项工作,但实际上只能做一项。不需要调用Release
,您只需调用Free
即可。我会这样写:
You've probably forgotten to assign
FormCreate
toOnCreate
. Personally I'd do it by overriding the constructor and so keeping the .dfm form out of the way.As an aside I would like to comment on the code you wrote:
You don't need to assign an owner to
DefForm
since you are taking on the task of cleaning up, although it generally does no harm to assign an owner. What's more thetry/finally
is try to do two jobs but it can only really do one. The call toRelease
is not needed, you can just callFree
.I'd write it like this:
您的意思是您的事件处理程序没有执行吗?
如果是这样,您是否可能只是忘记将过程分配给表单的 OnCreate 属性?
Do you mean, that your eventhandler is not executed?
If so, did you maybe just forgot to assign the procedure to the Form's OnCreate property?
好,我在这里有点困惑。您是在谈论表单还是框架?窗体有 OnCreate 处理程序,但框架没有。如果您想在创建框架时发生一些事情,请重写构造函数。
同样,框架没有 OnDestroy,因此如果需要清理任何内容,请确保覆盖析构函数。
OK, I'm a bit confused here. Are you talking about a form or a frame? Forms have an OnCreate handler, but frames do not. If you want to make something happen when a frame is created, override the constructor.
Similarly, frames have no OnDestroy, so make sure to override the destructor if there's anything you need to clean up.