简体   繁体   English

Delphi - 运行代码而不显示表单

[英]Delphi - Running code without showing form

What do you think about this programming practice: - I need to execute one transaction at first form and after that to force some updates that are placed at another form (for each item that is shown at another form). 您对此编程实践有何看法: - 我需要在第一个表单中执行一个事务,然后强制执行另一个表单中的某些更新(对于另一个表单中显示的每个项目)。 Ie it would be like show that form and click at some button. 即就像显示表单并单击某个按钮一样。 Because it is mandatory to execute these functionalities from second form, I thought to do it without showing second form. 因为从第二种形式执行这些功能是强制性的,所以我想在没有显示第二种形式的情况下这样做。 Is that good programming practice or you have some other recommendation? 这是一个很好的编程实践还是你有其他建议吗?

Also, is it enough just to set property> Visible:=False before ShowModal for the second form or I need to do some other actions? 另外,仅仅在ShowModal之前为第二个表单设置属性> Visible:= False还是我需要做一些其他操作?

Well, it's unusual to have a form that you don't show. 嗯,有一个你没有表现出来的表格是不寻常的。 Normally you separate your business logic from the UI. 通常,您将业务逻辑与UI分开。

To answer your question, I don't think you need to call ShowModal at all. 要回答你的问题,我认为你根本不需要打电话给ShowModal。 Just define a method on the form class and call that. 只需在表单类上定义一个方法并调用它。 Ultimately forms are just Delphi objects and you can use them as such. 最终形式只是Delphi对象,你可以这样使用它们。 If you don't want to show them, don't call ShowModal or Show. 如果您不想显示它们,请不要调用ShowModal或Show。

Second question first: Setting Visible := False is of no benefit because the point of all ShowXXX methods is to make the form visible. 第二个问题:设置Visible := False没有任何好处,因为所有ShowXXX方法的要点是使表单可见。 As David says, you could perform the actions without calling Show at all, provided of course your form doesn't rely on any OnActivate or OnShow code in order to do it's job properly. 正如David所说,你可以在不调用Show的情况下执行操作,前提是你的表单当然不依赖任何OnActivateOnShow代码来正常工作。

As for whether this is a good idea, I say no ! 至于这是否是一个好主意,我说没有

  • As I've already pointed out there is a concern you have to watch out for. 正如我已经指出的那样,你需要注意一个问题。 Ie that currently (or even due to maintenance at some point in the future) your form relies on being visible to do its job properly. 即目前(或者甚至由于未来某些时候的维护),您的表单依赖于可见其正常工作。
  • Of course, you could work around that by letting the form flicker open, and be programatically closed. 当然,您可以通过让表单闪烁打开并以编程方式关闭来解决这个问题。 Clearly an aesthetically poor choice. 显然是一个美学上差的选择。
  • Not to mention the problems of getting it right. 更不用说正确的问题了。 You'll end up writing a bunch of patch-work code to wrap the form so that it can do what you need to do, when you should rather do the following... 您最终会编写一堆补丁工作代码来包装表单,以便它可以执行您需要执行的操作,而您应该执行以下操作...

Correct Approach 正确的方法

  • Your form is currently doing at least 2 distinct things: 您的表单目前至少做了两件不同的事情:
    • Visual UI control (call it A) 可视UI控件(称之为A)
    • and "mandatory functionalities" (call it B) 和“强制性功能”(称之为B)
  • It doesn't matter much whether B is doing validation rules, extra processing, or whatever. B是在做验证规则,额外处理还是其他什么并不重要。
  • B is a process that does not require user interaction. B是不需要用户交互的过程。
  • Therefore, you need to: 因此,您需要:
    • Copy B into a non-UI location (either a simple unit with a custom object or a data module). 将B复制到非UI位置(带有自定义对象的简单单元或数据模块)。 Call it B* 叫它B *
    • Modify the form to call B* instead of using B. 修改表单以调用B *而不是使用B.
    • Test that your form still behaves correctly. 测试您的表单仍然正常运行。
    • Delete B 删除B.
    • And now you can have your new form call B* instead. 现在,您可以将新表单调用为B *。

The above approach will save you huge headaches in the future. 上述方法将为您节省巨大的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM