在Delphi / C ++ Builder中取消/中止创建新表单? [英] Cancel / abort creating a new form in Delphi / C++Builder?

查看:171
本文介绍了在Delphi / C ++ Builder中取消/中止创建新表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法可以从表单的OnCreate事件处理程序或C ++ Builder构造函数中取消或中止表单创建?

基本上,我希望能够从OnCreate或构造函数中调用Close(),并让它完全跳过显示表单。我有几种形式,作为它们初始化的一部分,可能确定它们不应该显示。 (我意识到我可以将这部分初始化分开,或者从调用窗体或类似项中添加额外的检查,但是如果有办法在OnCreate或构造函数中干净地完成所有这些操作,这似乎是最简单的。)



编辑:为回应一些评论,一些不显示任何逻辑是UI逻辑而不是业务逻辑;表单可能会在显示前显示确认信息,或者可能使用通用对话框来获取表单的输入,然后在用户取消该对话框时中止。 (有些是业务逻辑,需要重构,但是很难找到时间来重构所有需要的东西。) 解决方案

您可以在 OnCreate 处理程序中始终调用 Release ,但这会导致表单快速显示,然后关闭。不是一个非常专业的东西。



所以这是另一个想法。让表单具有公共职能或财产来回报他们是否实际上被展示。然后在那里你通常会有

  TheForm:= TSomeForm.Create(Self); 
TheForm.Show;

您将拥有

  TheForm:= TSomeForm.Create(Self); 
如果TheForm.ShouldAppear然后
TheForm.Show
else
TheForm.Release;

话虽如此 - 任何其他编码方式(所以你不会创建一个表单立即销毁)肯定会更好。特别是如果你想在UI和业务层之间保持清晰的分离,那么让代码决定是否将表单显示在表单之外会更好。只有在做出决定后才能创建表单。


Is there any way to cancel or abort form creation from within the form's OnCreate event handler or C++Builder constructor?

Basically, I'd like to be able to call Close() from OnCreate or from the constructor and have it skip showing the form altogether. I have several forms that as part of their initialization may determine that they shouldn't be shown at all. (I realize that I could split apart this portion of the initialization or add extra checks from the calling form or similar, but if there's a way to cleanly do all of this from within OnCreate or the constructor, that seems simplest.)

Edit: In response to a few comments, some of the don't-show-at-all logic is UI logic and not business logic; the form might display a confirmation before showing, or it might use a common dialog box to get input for the form then abort if the user cancels that dialog. (Some of it is business logic and needs to be refactored, but it's often hard to find the time to refactor everything that needs it.)

解决方案

You can always call Release in the OnCreate handler, but that will lead to the form quickly appearing and then being closed. Not a very professional thing.

So here's another idea. Let the forms have a public function or property to return whether they are in fact to be shown. Then where you would usually have

TheForm := TSomeForm.Create(Self);
TheForm.Show;

you would have

TheForm := TSomeForm.Create(Self);
if TheForm.ShouldAppear then
  TheForm.Show
else
  TheForm.Release;

Having said that - any other way of coding this (so you don't create a form that will be immediately destroyed) is surely better. Especially if you want to maintain a clear separation between UI and business layer it would be much better to have the code that decides whether the form is to be shown outside of the form. Create the form only after you have made the decision.

这篇关于在Delphi / C ++ Builder中取消/中止创建新表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆