将 VBA 中的 ProgressBar UserForms 显示为模态还是非模态更好? [英] Is it better to show ProgressBar UserForms in VBA as modal or modeless?

查看:7
本文介绍了将 VBA 中的 ProgressBar UserForms 显示为模态还是非模态更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VBA 中将 ProgressBar 用户窗体显示为模式还是非模式更好?在 VBA 中开发进度指标的最佳实践是什么?

Is it better to show ProgressBar UserForms in VBA as modal or modeless? What are the best practices for developing progress indicators in VBA?

无模式用户窗体需要使用 Application.Interactive = False,而模态用户窗体本质上会阻止与应用程序的任何交互,直到核心过程完成或被取消.

Modeless UserForms require the use of Application.Interactive = False, whereas Modal UserForms by their very nature block any interaction with the application until the core procedure has finished, or is cancelled.

如果使用Application.Interactive = False,但是,Esc键会中断代码执行,所以使用Application.EnableCancelKey = xlErrorHandler和错误处理(Err.Number = 18) 在用户窗体和调用过程中都是必需的.

If Application.Interactive = False is used, however, the Esc key interrupts code execution, so the use of Application.EnableCancelKey = xlErrorHandler and error handling (Err.Number = 18) is required in both the UserForm and the calling procedure.

资源密集型调用过程也可能导致 CommandButton_ClickUserForm_Activate 事件在无模式用户窗体中无法触发.

Resource intensive calling procedures can also result in CommandButton_Click and UserForm_Activate events misfiring in modeless UserForms.

一般来说,使用模态 UserForms 的进度指示器看起来更简单,因为正在执行的代码完全包含在 UserForm 模块中,并且不需要传递变量.

In general, progress indicators that use modal UserForms seem simpler, because the code that is being executed is fully contained in the UserForm module, and there is less need for passing of variables.

然而,使用模态 UserForms 作为进度指示器的问题是,每个需要进度指示器的过程都需要一个单独的 UserForm 模块,因为调用过程必须在 UserForm_Activate 过程中.

The problem, however, with using modal UserForms for progress indicators is that a separate UserForm module is required for every procedure that needs a progress indicator, because the calling procedure has to be inside the UserForm_Activate procedure.

因此,虽然可以在无模式用户窗体中拥有一个可重用的进度指示器,但它的可靠性不如在多个模式用户窗体中执行代码.

So, while it is possible to have a single reusable progress indicator in a modeless UserForm, it will be less reliable than executing the code from within multiple modal UserForms.

哪种方式更好?

谢谢!

推荐答案

我要结束这个并说 Modal 是赢家.我已经尝试了这两种方法,但你最终试图用无模式的用户表单来填补太多的漏洞.模态更难,因为它更严格,但它鼓励你将代码分解成更小的块,从长远来看这更好.

I am going to close this one out and say Modal is the winner. I have tried both ways, but you end up trying to close too many loopholes with modeless userforms. Modal is more difficult because it is more strict, but it encourages you to break up your code into smaller chunks which is better in the long run anyway.

这篇关于将 VBA 中的 ProgressBar UserForms 显示为模态还是非模态更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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