Delphi中的FormCreate问题

发布于 2024-11-07 08:25:53 字数 953 浏览 0 评论 0原文

我正在使用其他人的代码,并且正在添加一个新表单

所以,我已经创建了该表单,我可以打开它,使用按钮和列表等,但我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情深如许 2024-11-14 08:25:53

您可能忘记将 FormCreate 分配给 OnCreate。就我个人而言,我会通过重写构造函数来实现这一点,从而保持 .dfm 形式不受影响。

顺便说一句,我想对您编写的代码进行评论:

DefForm := TForm5.Create(Self);
Self.Visible := False;
try
  DefForm.ShowModal;
finally
  Self.Visible := True;
  DefForm.Release;
end;

您不需要为 DefForm 分配所有者,因为您正在承担清理任务,尽管它通常不会对指定一个所有者。更重要的是,try/finally 尝试做两项工作,但实际上只能做一项。不需要调用Release,您只需调用Free即可。

我会这样写:

DefForm := TForm5.Create(nil);
try
  Self.Visible := False;
  try
    DefForm.ShowModal;
  finally
    Self.Visible := True;
  end;
finally
  DefForm.Free;
end;

You've probably forgotten to assign FormCreate to OnCreate. 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:

DefForm := TForm5.Create(Self);
Self.Visible := False;
try
  DefForm.ShowModal;
finally
  Self.Visible := True;
  DefForm.Release;
end;

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 the try/finally is try to do two jobs but it can only really do one. The call to Release is not needed, you can just call Free.

I'd write it like this:

DefForm := TForm5.Create(nil);
try
  Self.Visible := False;
  try
    DefForm.ShowModal;
  finally
    Self.Visible := True;
  end;
finally
  DefForm.Free;
end;
决绝 2024-11-14 08:25:53

您的意思是您的事件处理程序没有执行吗?
如果是这样,您是否可能只是忘记将过程分配给表单的 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?

青巷忧颜 2024-11-14 08:25:53

网上查了一下,确实如此
好像没有OnCreate
函数,有一种方法可以覆盖
但似乎应该有一个
方法来定义当发生什么时
首先创建框架

好,我在这里有点困惑。您是在谈论表单还是框架?窗体有 OnCreate 处理程序,但框架没有。如果您想在创建框架时发生一些事情,请重写构造函数。

constructor TMyFrame.Create(AOwner: TComponent);
begin
 inherited Create(AOwner);
 RunList := CModelList.Create;
 RunList.ReadData;
 RunList.FillList(ListBox1.Items);
end;

同样,框架没有 OnDestroy,因此如果需要清理任何内容,请确保覆盖析构函数。

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

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.

constructor TMyFrame.Create(AOwner: TComponent);
begin
 inherited Create(AOwner);
 RunList := CModelList.Create;
 RunList.ReadData;
 RunList.FillList(ListBox1.Items);
end;

Similarly, frames have no OnDestroy, so make sure to override the destructor if there's anything you need to clean up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文