代码之家  ›  专栏  ›  技术社区  ›  SilentRevolution

用户窗体关闭事件

  •  7
  • SilentRevolution  · 技术社区  · 10 年前

    我有一个UserForm,它在条件存在时在循环中打开和关闭。用户可以单击几个按钮执行操作。问题是用户的不可预测性。其中一个问题是,用户不是单击其中一个按钮,而是单击UserForm顶部的关闭窗口按钮,这会在不执行操作的情况下进行循环。

    ---编辑---
    该按钮是否有一个事件,我可以用它执行代码,这样我就可以让它执行与表单本身的取消按钮相同的操作。我不需要隐藏或禁用它本身。

    2 回复  |  直到 10 年前
        1
  •  19
  •   Sam Gilbert    10 年前

    例如,您可以将下面的宏添加到UserForms代码模块:

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
        If CloseMode = vbFormControlMenu Then
            Cancel = True
            MsgBox "You can't close the dialog like this!"
        End If
    End Sub
    
        2
  •  3
  •   André Oliveira    7 年前

    您可以只关注一个按钮,而不是MsgBox:

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
        If CloseMode = vbFormControlMenu Then
            Cancel = True
            Me.Close_Button.SetFocus
        End If
    End Sub
    

    编辑: 我发现了一个更好的选择:

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    
        ' YOUR CODE HERE (Just copy whatever the close button does)
    
        If CloseMode = vbFormControlMenu Then
            Cancel = False
        End If
    
    End Sub
    
    推荐文章