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

发布于 2024-10-22 11:31:12 字数 264 浏览 1 评论 0原文

您对这种编程实践有何看法: - 我需要在第一个表单上执行一个事务,然后强制在另一个表单上执行一些更新(对于另一个表单中显示的每个项目)。即,就像显示该表单并单击某个按钮。因为必须从第二种形式执行这些功能,所以我想在不显示第二种形式的情况下执行此操作。这是好的编程习惯还是您有其他建议?

另外,仅仅设置属性>就足够了吗?对于第二种形式,在 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 技术交流群。

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

发布评论

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

评论(2

十秒萌定你 2024-10-29 11:31:12

好吧,有一个你不展示的形式是不寻常的。通常,您将业务逻辑与 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.

呆橘 2024-10-29 11:31:12

首先是第二个问题:设置 Visible := False 没有任何好处,因为所有 ShowXXX 方法的目的都是使表单可见。正如 David 所说,您可以在根本不调用 Show 的情况下执行这些操作,当然前提是您的表单不依赖任何 OnActivateOnShow 代码来完成其工作适当地。

至于这是否是一个好主意,我说

  • 正如我已经指出的,您必须注意一个问题。即,当前(甚至由于将来某个时候的维护)您的表单依赖于可见性才能正确完成其工作。
  • 当然,您可以通过让表单闪烁打开并以编程方式关闭来解决这个问题。显然这是一个不美观的选择。
  • 更不用说正确处理的问题了。当您应该执行以下操作时,您最终将编写一堆修补工作代码来包装表单,以便它可以完成您需要做的事情...

正确的方法

  • 您的表单是目前至少在做两件不同的事情:
    • 可视化 UI 控件(称之为 A)
    • 和“强制功能”(称之为 B)
  • B 是否执行验证规则、额外处理或其他什么并不重要。
  • B是一个不需要用户交互的进程。
  • 因此,您需要:
    • 将 B 复制到非 UI 位置(带有自定义对象的简单单元或数据模块)。称之为B*
    • 修改表单以调用 B* 而不是使用 B。
    • 测试您的表单是否仍能正常运行。
    • 删除B
    • 现在您可以让新表单调用 B*。

上述方法将让你在日后免去很多麻烦。

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 any OnActivate or OnShow code in order to do it's job properly.

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. I.e. 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)
    • and "mandatory functionalities" (call it B)
  • It doesn't matter much whether B is doing validation rules, extra processing, or whatever.
  • B is a process that does not require user interaction.
  • Therefore, you need to:
    • Copy B into a non-UI location (either a simple unit with a custom object or a data module). Call it B*
    • Modify the form to call B* instead of using B.
    • Test that your form still behaves correctly.
    • Delete B
    • And now you can have your new form call B* instead.

The above approach will save you huge headaches in the future.

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