VBA code to suppress the the control.Click() Sub / event handler

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-16 11:58:07

问题


OK. This is driving me nuts. I'm still getting a perpetual loop. My previous question got me so far >>> Using VBA code to modify OptionButton.value is activating the control.Click() Sub

I've rewritten some example code to try and nut this out. I've set up a Boolean 'handler' as per other suggestions (eg: Suppress events in UserForms). (I've called it SwitchOnEventResponder)

When it gets to the code that is causing the click event to fire, the handler flips back to True (which allows a value change to activate the _Click() sub) just as the OptionsDLG.OptionButton2.Value = True statement executes. WHY?

Code in the module:

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

UserForm "OptionsDLG" code:

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

回答1:


In order to understand the problem it will help to change your MsgBox message

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

Me refers to the current instance of an Object. Calling Unload Me will remove this instance from the Userforms collection. When you set a variable or control on a Userform that is not Loaded a new UserForm is auto-instantiated. The original instance of the Userform Me does not refer to the the new Instance of the UserForm.

Changing Me.SwitchOnEventResponder = False to OptionsDLG.SwitchOnEventResponder = False will solve the problem.

I prefer to create a new instance of the Userform before unloading the current instance. Using a With statement will ensure that all the variables and controls are fully qualified to the correct instance.

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

Unload Me


来源:https://stackoverflow.com/questions/47782832/vba-code-to-suppress-the-the-control-click-sub-event-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!