按下2个单选按钮时使某个按钮出现

时间:2018-11-06 14:21:00

标签: vb.net

我正在尝试在选中某些单选按钮组合时使按钮出现。

例如,当我按下2个单选按钮时,即使我为所有可能性都设置了if和多个elsif,也没有按钮出现。我希望当选择了两个以上的单选按钮时,将在“ Comencer”按钮所在的位置显示用于正确组合的正确按钮。

My program 我的代码:

Public Class Form1
Dim time As Integer
Dim score As Integer

'https://docs.microsoft.com/en-us/visualstudio/ide/step-2-create-a-random-addition-problem?view=vs-2017'

Private Sub lblEquations_Click(sender As Object, e As EventArgs) Handles lblEquations.Click

End Sub

'-------------------------------------------------------------------------------------------'

Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
    lblSecondes.Text = time
    time = time - 1

    If time = -1 Then
        Timer.Enabled = False
    End If
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    time = 10

    cmdCommencer1to12Divi.Visible = False
    cmdCommencer1to6Divi.Visible = False
    cmdCommencer6to12Divi.Visible = False
    cmdCommencer1to12Multi.Visible = False
    cmdCommencer1to6Multi.Visible = False
    cmdCommencer6to12Multi.Visible = False
    cmdCommencer1to12Plus.Visible = False
    cmdCommencer1to6plus.Visible = False
    cmdCommencer6to12Plus.Visible = False
    cmdCommencer1to12Sous.Visible = False
    cmdCommencer1to6Sous.Visible = False
    cmdCommencer6to12Sous.Visible = False


    '-------------------------------------------------------------------------------------------'


    If RadBut1a12.Checked And RadButAddition.Checked = True Then
        cmdCommencer1to12Plus.Visible = True

        cmdCommencer1to12Divi.Visible = False
        cmdCommencer1to6Divi.Visible = False
        cmdCommencer6to12Divi.Visible = False
        cmdCommencer1to12Multi.Visible = False
        cmdCommencer1to6Multi.Visible = False
        cmdCommencer6to12Multi.Visible = False
        cmdCommencer1to6plus.Visible = False
        cmdCommencer6to12Plus.Visible = False
        cmdCommencer1to12Sous.Visible = False
        cmdCommencer1to6Sous.Visible = False
        cmdCommencer6to12Sous.Visible = False

    ElseIf RadBut1a12.Checked And RadButDivision.Checked = True Then
        cmdCommencer1to12Divi.Visible = True

        cmdCommencer1to6Divi.Visible = False
        cmdCommencer6to12Divi.Visible = False
        cmdCommencer1to12Multi.Visible = False
        cmdCommencer1to6Multi.Visible = False
        cmdCommencer6to12Multi.Visible = False
        cmdCommencer1to12Plus.Visible = False
        cmdCommencer1to6plus.Visible = False
        cmdCommencer6to12Plus.Visible = False
        cmdCommencer1to12Sous.Visible = False
        cmdCommencer1to6Sous.Visible = False
        cmdCommencer6to12Sous.Visible = False

    ElseIf RadBut1a12.Checked And RadButMultiplication.Checked = True Then
        cmdCommencer1to12Multi.Visible = True

        cmdCommencer1to12Divi.Visible = False
        cmdCommencer1to6Divi.Visible = False
        cmdCommencer6to12Divi.Visible = False
        cmdCommencer1to6Multi.Visible = False
        cmdCommencer6to12Multi.Visible = False
        cmdCommencer1to12Plus.Visible = False
        cmdCommencer1to6plus.Visible = False
        cmdCommencer6to12Plus.Visible = False
        cmdCommencer1to12Sous.Visible = False
        cmdCommencer1to6Sous.Visible = False
        cmdCommencer6to12Sous.Visible = False

    ElseIf RadBut1a12.Checked And RadButSoustraction.Checked = True Then
        cmdCommencer1to12Sous.Visible = True

        cmdCommencer1to12Divi.Visible = False
        cmdCommencer1to6Divi.Visible = False
        cmdCommencer6to12Divi.Visible = False
        cmdCommencer1to12Multi.Visible = False
        cmdCommencer1to6Multi.Visible = False
        cmdCommencer6to12Multi.Visible = False
        cmdCommencer1to12Plus.Visible = False
        cmdCommencer1to6plus.Visible = False
        cmdCommencer6to12Plus.Visible = False
        cmdCommencer1to6Sous.Visible = False
        cmdCommencer6to12Sous.Visible = False


        '-------------------------------------------------------------------------------------------'


    ElseIf RadBut1a6.Checked And RadButAddition.Checked = True Then
        cmdCommencer1to6plus.Visible = True

        cmdCommencer1to12Divi.Visible = False
        cmdCommencer1to6Divi.Visible = False
        cmdCommencer6to12Divi.Visible = False
        cmdCommencer1to12Multi.Visible = False
        cmdCommencer1to6Multi.Visible = False
        cmdCommencer6to12Multi.Visible = False
        cmdCommencer1to12Plus.Visible = False
        cmdCommencer6to12Plus.Visible = False
        cmdCommencer1to12Sous.Visible = False
        cmdCommencer1to6Sous.Visible = False
        cmdCommencer6to12Sous.Visible = False

    ElseIf RadBut1a6.Checked And RadButDivision.Checked = True Then
        cmdCommencer1to6Divi.Visible = True

        cmdCommencer1to12Divi.Visible = False
        cmdCommencer6to12Divi.Visible = False
        cmdCommencer1to12Multi.Visible = False
        cmdCommencer1to6Multi.Visible = False
        cmdCommencer6to12Multi.Visible = False
        cmdCommencer1to12Plus.Visible = False
        cmdCommencer1to6plus.Visible = False
        cmdCommencer6to12Plus.Visible = False
        cmdCommencer1to12Sous.Visible = False
        cmdCommencer1to6Sous.Visible = False
        cmdCommencer6to12Sous.Visible = False

    ElseIf RadBut1a6.Checked And RadButMultiplication.Checked = True Then
        cmdCommencer1to6Multi.Visible = True

        cmdCommencer1to12Divi.Visible = False
        cmdCommencer1to6Divi.Visible = False
        cmdCommencer6to12Divi.Visible = False
        cmdCommencer1to12Multi.Visible = False
        cmdCommencer6to12Multi.Visible = False
        cmdCommencer1to12Plus.Visible = False
        cmdCommencer1to6plus.Visible = False
        cmdCommencer6to12Plus.Visible = False
        cmdCommencer1to12Sous.Visible = False
        cmdCommencer1to6Sous.Visible = False
        cmdCommencer6to12Sous.Visible = False

    ElseIf RadBut1a6.Checked And RadButSoustraction.Checked = True Then
        cmdCommencer1to6Sous.Visible = True

        cmdCommencer1to12Divi.Visible = False
        cmdCommencer1to6Divi.Visible = False
        cmdCommencer6to12Divi.Visible = False
        cmdCommencer1to12Multi.Visible = False
        cmdCommencer1to6Multi.Visible = False
        cmdCommencer6to12Multi.Visible = False
        cmdCommencer1to12Plus.Visible = False
        cmdCommencer1to6plus.Visible = False
        cmdCommencer6to12Plus.Visible = False
        cmdCommencer1to12Sous.Visible = False
        cmdCommencer6to12Sous.Visible = False


        '-------------------------------------------------------------------------------------------'


    ElseIf RadBut6a12.Checked And RadButAddition.Checked = True Then
        cmdCommencer6to12Plus.Visible = True

        cmdCommencer1to12Divi.Visible = False
        cmdCommencer1to6Divi.Visible = False
        cmdCommencer6to12Divi.Visible = False
        cmdCommencer1to12Multi.Visible = False
        cmdCommencer1to6Multi.Visible = False
        cmdCommencer6to12Multi.Visible = False
        cmdCommencer1to12Plus.Visible = False
        cmdCommencer1to6plus.Visible = False
        cmdCommencer1to12Sous.Visible = False
        cmdCommencer1to6Sous.Visible = False
        cmdCommencer6to12Sous.Visible = False

    ElseIf RadBut6a12.Checked And RadButDivision.Checked = True Then
        cmdCommencer6to12Divi.Visible = True

        cmdCommencer1to12Divi.Visible = False
        cmdCommencer1to6Divi.Visible = False
        cmdCommencer1to12Multi.Visible = False
        cmdCommencer1to6Multi.Visible = False
        cmdCommencer6to12Multi.Visible = False
        cmdCommencer1to12Plus.Visible = False
        cmdCommencer1to6plus.Visible = False
        cmdCommencer6to12Plus.Visible = False
        cmdCommencer1to12Sous.Visible = False
        cmdCommencer1to6Sous.Visible = False
        cmdCommencer6to12Sous.Visible = False

    ElseIf RadBut6a12.Checked And RadButMultiplication.Checked = True Then
        cmdCommencer6to12Multi.Visible = True

        cmdCommencer1to12Divi.Visible = False
        cmdCommencer1to6Divi.Visible = False
        cmdCommencer6to12Divi.Visible = False
        cmdCommencer1to12Multi.Visible = False
        cmdCommencer1to6Multi.Visible = False
        cmdCommencer1to12Plus.Visible = False
        cmdCommencer1to6plus.Visible = False
        cmdCommencer6to12Plus.Visible = False
        cmdCommencer1to12Sous.Visible = False
        cmdCommencer1to6Sous.Visible = False
        cmdCommencer6to12Sous.Visible = False

    ElseIf RadBut6a12.Checked And RadButSoustraction.Checked = True Then
        cmdCommencer6to12Sous.Visible = True

        cmdCommencer1to12Divi.Visible = False
        cmdCommencer1to6Divi.Visible = False
        cmdCommencer6to12Divi.Visible = False
        cmdCommencer1to12Multi.Visible = False
        cmdCommencer1to6Multi.Visible = False
        cmdCommencer6to12Multi.Visible = False
        cmdCommencer1to12Plus.Visible = False
        cmdCommencer1to6plus.Visible = False
        cmdCommencer6to12Plus.Visible = False
        cmdCommencer1to12Sous.Visible = False
        cmdCommencer1to6Sous.Visible = False

    End If
End Sub



Public Sub StartQuiz1to6Plus()
    addend1 = randomizer.Next(6)
    addend2 = randomizer.Next(6)

    lblEquations.Text = addend1.ToString() & " + " & addend2.ToString()
End Sub

Public Sub StartQuiz6to12Plus()
    addend1 = randomizer.Next(6, 12)
    addend2 = randomizer.Next(6, 12)

    lblEquations.Text = addend1.ToString() & " + " & addend2.ToString()
End Sub

Public Sub StartQuiz1to12Plus()
    addend1 = randomizer.Next(12)
    addend2 = randomizer.Next(12)

    lblEquations.Text = addend1.ToString() & " + " & addend2.ToString()
End Sub



'-------------------------------------------------------------------------------------------'

Public Sub StartQuiz1to6Sous()
    addend1 = randomizer.Next(6)
    addend2 = randomizer.Next(6)

    lblEquations.Text = addend1.ToString() & " - " & addend2.ToString()
End Sub

Public Sub StartQuiz6to12Sous()
    addend1 = randomizer.Next(6, 12)
    addend2 = randomizer.Next(6, 12)

    lblEquations.Text = addend1.ToString() & " - " & addend2.ToString()
End Sub

Public Sub StartQuiz1to12Sous()
    addend1 = randomizer.Next(12)
    addend2 = randomizer.Next(12)

    lblEquations.Text = addend1.ToString() & " - " & addend2.ToString()
End Sub

'-------------------------------------------------------------------------------------------'

Public Sub StartQuiz1to6Multi()
    addend1 = randomizer.Next(6)
    addend2 = randomizer.Next(6)

    lblEquations.Text = addend1.ToString() & " x " & addend2.ToString()
End Sub

Public Sub StartQuiz6to12Multi()
    addend1 = randomizer.Next(6, 12)
    addend2 = randomizer.Next(6, 12)

    lblEquations.Text = addend1.ToString() & " x " & addend2.ToString()
End Sub

Public Sub StartQuiz1to12Multi()
    addend1 = randomizer.Next(12)
    addend2 = randomizer.Next(12)

    lblEquations.Text = addend1.ToString() & " x " & addend2.ToString()
End Sub

'-------------------------------------------------------------------------------------------'

Public Sub StartQuiz1to6Divi()
    addend1 = randomizer.Next(6)
    addend2 = randomizer.Next(6)

    lblEquations.Text = addend1.ToString() & "  " & addend2.ToString()
End Sub

Public Sub StartQuiz6to12Divi()
    addend1 = randomizer.Next(6, 12)
    addend2 = randomizer.Next(6, 12)

    lblEquations.Text = addend1.ToString() & "  " & addend2.ToString()
End Sub

Public Sub StartQuiz1to12Divi()
    addend1 = randomizer.Next(12)
    addend2 = randomizer.Next(12)

    lblEquations.Text = addend1.ToString() & "  " & addend2.ToString()
End Sub








Private randomizer As New Random()

Private addend1 As Integer
Private addend2 As Integer

Public Sub TimerReset()
    time = 10
End Sub

Private Sub txtReponse_TextChanged(sender As Object, e As EventArgs) Handles txtReponse.TextChanged
    If txtReponse.Text = (addend1 + addend2) Then
        TimerReset()
        score = score + 1
        lblScore.Text = score
        StartQuiz1to6Plus()
        txtReponse.Text = ""

    End If
End Sub

Private Sub cmdQuitter_Click(sender As Object, e As EventArgs) Handles cmdQuitter.Click
    End
End Sub

Private Sub cmdRestart_Click(sender As Object, e As EventArgs) Handles cmdRestart.Click
    Timer.Enabled = False
    TimerReset()
    lblEquations.Text = ""
    lblScore.Text = ""
    lblSecondes.Text = ""
    txtReponse.Text = ""
End Sub

Private Sub cmdCommencer1to6plus_Click(sender As Object, e As EventArgs) Handles cmdCommencer1to6plus.Click
    If cmdCommencer1to6plus.Text = "Commencer" Then
        cmdCommencer1to6plus.Text = "Arrêter"

        TimerReset()
        Timer.Enabled = True
        StartQuiz1to6Plus()

        If time = 0 Then
            Timer.Enabled = False
        End If


    ElseIf cmdCommencer1to6plus.Text = "Arrêter" Then
        TimerReset()
        Timer.Enabled = False
    End If
End Sub

Private Sub cmdCommencer1to12Divi_Click(sender As Object, e As EventArgs) Handles cmdCommencer1to12Divi.Click
    If cmdCommencer1to12Divi.Text = "Commencer" Then
        cmdCommencer1to12Divi.Text = "Arrêter"

        TimerReset()
        Timer.Enabled = True
        StartQuiz1to12Divi()

        If time = 0 Then
            Timer.Enabled = False
        End If


    ElseIf cmdCommencer1to12Divi.Text = "Arrêter" Then
        TimerReset()
        Timer.Enabled = False
    End If
End Sub

Private Sub cmdCommencer6to12Divi_Click(sender As Object, e As EventArgs) Handles cmdCommencer6to12Divi.Click

End Sub

Private Sub cmdCommencer1to6Divi_Click(sender As Object, e As EventArgs) Handles cmdCommencer1to6Divi.Click

End Sub

Private Sub cmdCommencer1to12Multi_Click(sender As Object, e As EventArgs) Handles cmdCommencer1to12Multi.Click

End Sub

Private Sub cmdCommencer6to12Multi_Click(sender As Object, e As EventArgs) Handles cmdCommencer6to12Multi.Click

End Sub

Private Sub cmdCommencer1to6Multi_Click(sender As Object, e As EventArgs) Handles cmdCommencer1to6Multi.Click

End Sub

Private Sub cmdCommencer1to12Sous_Click(sender As Object, e As EventArgs) Handles cmdCommencer1to12Sous.Click

End Sub

Private Sub cmdCommencer6to12Sous_Click(sender As Object, e As EventArgs) Handles cmdCommencer6to12Sous.Click

End Sub

Private Sub cmdCommencer1to6Sous_Click(sender As Object, e As EventArgs) Handles cmdCommencer1to6Sous.Click

End Sub

Private Sub cmdCommencer1to12Plus_Click(sender As Object, e As EventArgs) Handles cmdCommencer1to12Plus.Click

End Sub

Private Sub cmdCommencer6to12Plus_Click(sender As Object, e As EventArgs) Handles cmdCommencer6to12Plus.Click

End Sub

Private Sub RadButAddition_CheckedChanged(sender As Object, e As EventArgs) Handles RadButAddition.CheckedChanged

End Sub

结束班级

1 个答案:

答案 0 :(得分:1)

好吧,我们会保持简单,因为您显然是个初学者。如果您觉得自己不明白我在说什么,可以提出问题。

将您的按钮放在表单上,​​但将其设置为不可见。

.Visible = False

由于单选按钮通常是互斥的,因此我将继续使用CheckBoxes,但是您可以对单选按钮使用相同的代码,但要进行较小的更改。

将此事件添加到每个复选框:

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    CheckForCheckBoxCombinaison()
End Sub

此事件将调用Sub处理检查。您可能已经在事件处理程序中正确编写了代码,但是您不得不将其复制和粘贴几次,并且您并不想凭经验在同一个项目中的多个位置复制和粘贴代码。

现在创建另一个我们称为的Sub:

Private Sub CheckForCheckBoxCombinaison()
    If CheckBox1.Checked AndAlso CheckBox3.Checked Then
        Button1.Visible = True
    Else
        Button1.Visible = False
    End If
End Sub

是的,玩得开心。