【发布时间】: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 - 两者都有。没有好的理由。我想我只需要在用户表单代码中使用它。