Delphi - 运行代码而不显示表单
您对这种编程实践有何看法: - 我需要在第一个表单上执行一个事务,然后强制在另一个表单上执行一些更新(对于另一个表单中显示的每个项目)。即,就像显示该表单并单击某个按钮。因为必须从第二种形式执行这些功能,所以我想在不显示第二种形式的情况下执行此操作。这是好的编程习惯还是您有其他建议?
另外,仅仅设置属性>就足够了吗?对于第二种形式,在 ShowModal 之前 Visible:=False 或者我需要执行一些其他操作?
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). I.e. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,有一个你不展示的形式是不寻常的。通常,您将业务逻辑与 UI 分开。
要回答您的问题,我认为您根本不需要调用 ShowModal。只需在表单类上定义一个方法并调用它即可。最终,表单只是 Delphi 对象,您可以这样使用它们。如果您不想显示它们,请不要调用 ShowModal 或 Show。
Well, it's unusual to have a form that you don't show. Normally you separate your business logic from the UI.
To answer your question, I don't think you need to call ShowModal at all. Just define a method on the form class and call that. Ultimately forms are just Delphi objects and you can use them as such. If you don't want to show them, don't call ShowModal or Show.
首先是第二个问题:设置
Visible := False
没有任何好处,因为所有 ShowXXX 方法的目的都是使表单可见。正如 David 所说,您可以在根本不调用 Show 的情况下执行这些操作,当然前提是您的表单不依赖任何OnActivate
或OnShow
代码来完成其工作适当地。至于这是否是一个好主意,我说不!
正确的方法
上述方法将让你在日后免去很多麻烦。
Second question first: Setting
Visible := False
is of no benefit because the point of all ShowXXX methods is to make the form visible. As David says, you could perform the actions without calling Show at all, provided of course your form doesn't rely on anyOnActivate
orOnShow
code in order to do it's job properly.As for whether this is a good idea, I say no!
Correct Approach
The above approach will save you huge headaches in the future.