无模式形式仍会暂停代码执行 [英] Modeless form that still pauses code execution

查看:107
本文介绍了无模式形式仍会暂停代码执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总有没有一种用户模式可以无模式运行,同时仍然像模式模式一样暂停代码执行?

Is there anyway to have a userform that acts modeless, while still pausing code execution like a modal form?

我希望显示用户窗体,但仍允许与父程序进行交互.模态形式会阻止与父程序的交互.无模式表单可以工作,但是我希望在表单启动时暂停代码执行.

I'd like the userform to show, but still allow interaction with the parent program. Modal forms block interaction with the parent program. A modeless form would work, but I would like the code execution to pause while the form is up.

我已经通过创建一个无限循环来解决此问题,该循环检查表单是否可见,但这似乎有点hacky.

I've worked around this by creating an infinite loop that checks if the form is visible, but that seems a bit hacky.

Public Sub GetFormInfoAndDoStuff    
  ufForm.show vbModeless

  Do while ufForm.Visible
    DoEvents
  Loop

  ' Do other stuff dependent on form 
End Sub

已编辑,以阐明存在.show之后的代码,该代码必须在完成用户表单后执行

推荐答案

您应该能够将表单显示为vbModeless,并且仅在特别要求时(例如,从CommandButton或其他控件中)执行代码.

You should be able display the form as vbModeless and only execute code when specifically requested, i.e., from a CommandButton or other control.

然后,您可以通过"X"按钮或另一个调用UserForm_Terminate事件的控件使窗体保持可见/显示状态,直到明确将其关闭为止.

You then leave the form visible/shown until it is specifically closed, via the "X" button or via another control which calls the UserForm_Terminate event.

为此,您可能需要将一些可执行代码移入另一个子例程和/或模块,并例如从CommandButton_Click事件中调用该子例程.

In order to achieve this, you may need to move some of your executable code in to another subroutine and/or module, and call this subroutine for example from a CommandButton_Click event.

您已经在某个子例程中包含了如下一行:

You already have a subroutine somewhere that contains a line like:

Sub ShowTheForm()

    UserForm1.Show vbModeless
End Sub

因此,表单将正确显示,以允许用户输入到父应用程序.

So the form is displayed properly to allow user-input to the parent application.

您实际上不需要在上述模块中放入任何其他代码.我们将其他代码放在其他模块/子程序中,然后从用户控件(如命令按钮)中调用它.

You don't really need to put any other code in the above module. We will put the other code in other modules/subs, and then call it from user controls like command buttons.

示例:

获取所有可执行代码,然后将其放入另一个子例程(如果适合您的组织偏好,则放入另一个模块),例如:

Take all of your executable code, and put it in another subroutine (and if it suits your organizational preference, another module), like:

Sub MyMacro(msg$)
    MsgBox msg
End Sub

在用户窗体上,添加一个命令按钮并为其分配以下代码:

On the UserForm, add a command button and assign it the following code:

Sub CommandButton1_Click()
    MyMacro "hello"
End Sub

现在,将显示该表单,直到用户单击"X"按钮.仅当从命令按钮调用时,代码才会运行.

Now, the form will display until the user clicks the "X" button. Code will only run when called from the command button.

编辑说明

您无需使用此方法暂停"执行.一旦无模式显示该表单,执行就会结束,并且该表单将继续存在.该对象具有一些事件,您可以使用这些事件来触发代码的进一步执行.

You don't need to "pause" the execution using this method. Execution ends once the form is displayed modelessly, and the form persists. The object has some events which you may use to trigger further execution of code.

这篇关于无模式形式仍会暂停代码执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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