[英]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的情况下执行操作,前提是你的表单当然不依赖任何OnActivate
或OnShow
代码来正常工作。
As for whether this is a good idea, I say no ! 至于这是否是一个好主意,我说没有 !
Correct Approach 正确的方法
The above approach will save you huge headaches in the future. 上述方法将为您节省巨大的麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.