找不到此操作的代码

时间:2016-11-04 14:29:03

标签: vb.net

如果Label3.Text显示不正确的,我试图这样做,那么表单的背景颜色会变为红色,如果它显示正确,那么它会变成绿色

Public Class Form1
    'Here I am declaring the varibles globally across the code
    Dim number1, number2 As Integer
    Dim rn As New Random
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'When the form loads the lebel 1 and label 2 will already load with random numbers between 0 - 100
        number1 = rn.Next(0, 100)
        number2 = rn.Next(0, 100)
        Label1.Text = number1
        Label2.Text = number2
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'When you click the button it will then give you a new set of random numbers
        number1 = rn.Next(0, 100)
        number2 = rn.Next(0, 100)
        Label1.Text = number1
        Label2.Text = number2
    End Sub

    Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
        'The operators is making sure that the 2 numbers are equal to each other and then if its equal
        'it will say it is correct or if it is false it will show incorrect
        If number1 = number2 Then
            Label3.Text = "Correct"
        Else
            Label3.Text = "Incorrect"
        End If
    End Sub

    Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
        'The operators is making sure that the 2 numbers are equal to each other and then if its equal
        'it will say it is correct or if it is false it will show incorrect
        If number1 <> number2 Then
            Label3.Text = "Correct"
        Else
            Label3.Text = "Incorrect"
        End If
    End Sub

    Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
        'The operators is making sure that the left number is smaller than
        'the value on the right and if thats true
        'it will say it is correct or if it is false it will show incorrect
        If number1 < number2 Then
            Label3.Text = "Correct"
        Else
            Label3.Text = "Incorrect"
        End If
    End Sub

    Private Sub RadioButton4_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged
        'The operators is making sure that the left number is smaller than
        'the value on the right or if it equal and if thats true
        'it will say it is correct or if it is false it will show incorrect
        If number1 <= number2 Then
            Label3.Text = "Correct"
        Else
            Label3.Text = "Incorrect"
        End If
    End Sub

    Private Sub RadioButton5_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton5.CheckedChanged
        'The operators is making sure that the left number is bigger than
        'the value on the right and if thats true
        'it will say it is correct or if it is false it will show incorrect
        If number1 > number2 Then
            Label3.Text = "Correct"
        Else
            Label3.Text = "Incorrect"
        End If
    End Sub

    Private Sub RadioButton6_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton6.CheckedChanged
        'The operators is making sure that the left number is bigger than
        'the value on the right or if it equal and if thats true
        'it will say it is correct or if it is false it will show incorrect
        If number1 >= number2 Then
            Label3.Text = "Correct"
        Else
            Label3.Text = "Incorrect"
        End If
    End Sub

    Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click

    End Sub

    Private Sub Label3_EnabledChanged(sender As Object, e As EventArgs) Handles Label3.EnabledChanged
        If Label3.Text = "Incorrect" Then
            Me.BackColor = Color.AliceBlue
        Else
            Label3.Text = "Correct"
            Me.BackColor = Color.Coral
        End If
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

将此添加到您的代码中

Private Sub Label3_TextChanged(sender As Object, e As EventArgs) Handles Label3.TextChanged
    If Label3.Text = "Incorrect" Then
        Me.BackColor = Color.Red
    ElseIf Label3.Text = "Correct" Then
        Me.BackColor = Color.Green
    End If
End Sub

如果你发现自己一遍又一遍地编写非常相似的代码,你应该把它放在一个方法中。想想您的表单是否有两种状态:正确和错误。在每个状态中,UI具有特定的外观。如果正确,标签说明正确,表格为绿色。如果不正确,标签显示不正确,表格为红色。因此,您可以使用此方法代替我在上面提供的TextChanged处理程序。

Private Sub updateUI(isCorrect As Boolean)
    If isCorrect Then
        Label3.Text = "Correct"
        Me.BackColor = Color.Green
    Else
        Label3.Text = "Incorrect"
        Me.BackColor = Color.Red
    End If
End Sub

这极大地简化了单选按钮处理程序。在代码和可维护性方面

Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
    updateUI(number1 = number2)
End Sub

Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
    updateUI(number1 <> number2)
End Sub

Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
    updateUI(number1 < number2)
End Sub

Private Sub RadioButton4_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged
    updateUI(number1 <= number2)
End Sub

Private Sub RadioButton5_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton5.CheckedChanged
    updateUI(number1 > number2)
End Sub

Private Sub RadioButton6_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton6.CheckedChanged
    updateUI(number1 >= number2)
End Sub