0

这次我正在制作选项框,所以我在答案周围输入了带有语音标记的代码,然后我尝试使用它,但它不起作用。我输入的代码是:

Private Sub OptionButton1_Click()
    If OptionButton1.Value = "Stores all the components" Then
        MsgBox "That is correct. Well done!"
        SlideShowWindows(1).View.Next
    Else
        MsgBox "Sorry, that is not right. Try again"
    End If
End Sub

请问有人可以帮我吗?

4

3 回答 3

1

我想你正在尝试这个

If OptionButton1.Caption = "Stores all the components" 

或这个

If OptionButton1.Value = True Then
于 2013-10-10T17:13:45.797 回答
0

或许更漂亮一点...

每个 OptionButton 点击​​事件调用 CheckAnswer:

Private Sub OptionButton1_Click()
    If CheckAnswer(Me.OptionButton1, "Correct answer") Then
        MsgBox "Good user.  GOOD!"
    Else
        MsgBox "Bad user!  BAD!  Go stand in the corner."
    End If
End Sub

Private Sub OptionButton2_Click()
    If CheckAnswer(Me.OptionButton2, "Correct answer") Then
        MsgBox "Good user.  GOOD!"
    Else
        MsgBox "Bad user!  BAD!  Go stand in the corner."
    End If
End Sub

Function CheckAnswer(oCtl As Object, sCorrectAnswer As String) As Boolean
    If UCase(oCtl.Caption) = UCase(sCorrectAnswer) Then
        CheckAnswer = True
    Else
        CheckAnswer = False
    End If
End Function
于 2013-10-11T01:18:07.593 回答
0

VBA 中的 optionButton 的值为 0(未选中)或 -1(选中)。如果您正在寻找选项按钮所说的内容,我相信您想要 .Caption 而不是 .Value

编辑:再次查看您的代码后,我发现了一个根本缺陷。您将此分配给仅选项单击,因此如果第一个按钮没有正确的值,则不会发生任何事情。我认为您需要的是这样的子程序

Private Sub checkAnswer (answer as string)
    dim expecTedAnswer as string
    expectedAnswer = "The correct answer"
    if answer = expectedanswer then
        msgbox "Correct"
    else
        msgbox "Incorrect"
    end if
End sub

然后在选项中单击,只需调用 Sub

CheckAnswer(OptionButton1.Caption)

请注意,这非常难看(您可能希望创建一个易于使用的调用方法和设置正确答案的方法),但它应该给您一些继续。

于 2013-10-10T17:16:49.813 回答