【问题标题】:VBA code to suppress the the control.Click() Sub / event handlerVBA 代码抑制 control.Click() 子/事件处理程序
【发布时间】:2017-12-12 22:47:36
【问题描述】:

好的。这让我发疯了。我仍然得到一个永久循环。 我之前的问题让我到目前为止>>> Using VBA code to modify OptionButton.value is activating the control.Click() Sub

我已经重写了一些示例代码来尝试解决这个问题。 我根据其他建议设置了一个布尔“处理程序” (例如:Suppress events in UserForms)。 (我称之为SwitchOnEventResponder

当它到达导致 click 事件触发的代码时,处理程序会翻转回 True(允许更改值以激活 _Click() 子),就像 OptionsDLG.OptionButton2.Value = True 语句执行。为什么?

模块中的代码:

Sub StartDLG()
Call changeframe(135)
'code to change the UserForm (to hide the text box)
Call PopulateText
OptionsDLG.Show
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = 0 Then Cancel = True
End Sub

Sub PopulateText()
' Populates text box with initial text
OptionsDLG.TextBox1.Value = "Enter your msg here"
End Sub

Sub changeframe(FormH As String)

OptionsDLG.Height = FormH
OptionsDLG.Label1.Visible = False
OptionsDLG.TextBox1.Visible = False
End Sub

用户窗体“OptionsDLG”代码:

Public SwitchOnEventResponder As Boolean

Private Sub UserForm_Initialize()
        Me.SwitchOnEventResponder = True
End Sub

Private Sub OptionButton2_Change()
        MsgBox "Me.SwitchOnEventResponder = " & Me.SwitchOnEventResponder
        If Me.SwitchOnEventResponder = False Then
            Exit Sub
        End If
    End Sub

Private Sub OptionButton1_Click()

 Unload Me
 Call changeframe(135)
 'code to change the UserForm
 Call PopulateText
 OptionsDLG.Show

End Sub

Private Sub OptionButton2_Click()
 If Me.SwitchOnEventResponder = False Then
            Exit Sub
 End If

 Unload Me

 Call PopulateText

 Me.SwitchOnEventResponder = False
 MsgBox "Me.SwitchOnEventResponder = " & Me.SwitchOnEventResponder
 OptionsDLG.OptionButton1.Value = False
 OptionsDLG.OptionButton2.Value = True '<< This is where the loop starts. grrrr
 Me.SwitchOnEventResponder = True
 OptionsDLG.Show
End Sub

' This runs when the Ok button is clicked
Private Sub OK_Click()
    If OptionButton1.Value = True Then MsgBox "HAPPY"
    If OptionButton2.Value = True Then MsgBox "HAPPIER"
End Sub

' This runs when the cancel button is clicked
Private Sub Cancel_Click()
    ' Show the main dialogue
    Stop
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
            If CloseMode = 0 Then Cancel = True
End Sub

【问题讨论】:

  • 尝试在单独的模块中声明Public SwitchOnEventResponder As Boolean,而不是在用户表单中。我以这种方式使用它并且它的工作原理。
  • 只是出于好奇,为什么 Private Sub Userform_QueryClose 在模块中而不是在用户窗体中?
  • @Sphinx - 我将声明从用户表单移到模块中。现在用户表单无法识别Me.SwitchOnEventResponder = True 语句
  • @SilentRevolution - 两者都有。没有好的理由。我想我只需要在用户表单代码中使用它。

标签: vba excel


【解决方案1】:

为了了解问题,更改您的MsgBox 消息会有所帮助

MsgBox "Me.SwitchOnEventResponder = " & Me.SwitchOnEventResponder & vbCrLf & "OptionsDLG.SwitchOnEventResponder = " & OptionsDLG.SwitchOnEventResponder

Me 指的是对象的当前实例。调用 Unload Me 将从 Userforms 集合中删除此实例。当您在未加载的用户窗体上设置变量或控件时,将自动实例化新的用户窗体。用户窗体的原始实例Me 不引用用户窗体的新实例。

Me.SwitchOnEventResponder = False 更改为OptionsDLG.SwitchOnEventResponder = False 即可解决问题。

我更喜欢在卸载当前实例之前创建用户窗体的新实例。使用 With 语句将确保所有变量和控件都完全符合正确的实例。

With New OptionsDLG
    .SwitchOnEventResponder = False
    .OptionButton1.Value = False
    .OptionButton2.Value = True
    .SwitchOnEventResponder = True
    .Show
End With

Unload Me

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 2023-03-10
    • 2015-03-17
    相关资源
    最近更新 更多