如果发生阻塞,如何简化复杂的4k线?

时间:2018-07-02 14:18:01

标签: vba if-statement optimization cascade

我已经尝试过了,但是不喜欢 可能是白色的“周期前或周期内”
我使用Visual Basic 2016

我有10个复选框,从0到9的3个随机数,
如果3个复选框为白色,则检查与“随机生成”相同的3个数字,文本框变为 “与3号相同”

这是复选框和文本框的img:Link
这是一个巨大的简化

        If primoR = 0 Then
        If CheckBox10.Checked Then
            If secondoR = 1 Then
                If CheckBox1.Checked Then
                    If terzoR = 2 Then
                        If CheckBox2.Checked Then
                            TextBox1.BackColor = Color.Green
                        End If
                    ElseIf terzoR = 3 Then
                        If CheckBox3.Checked Then
                            TextBox1.BackColor = Color.Green
                        End If
                    Else
                        TextBox1.BackColor = Color.Black
                    End If
                End If
            End If

        End If
    ElseIf primoR = 1 Then
        If secondoR = 1 Then
            If CheckBox1.Checked Then
                If terzoR = 2 Then
                    If CheckBox2.Checked Then
                        TextBox1.BackColor = Color.Green
                    End If
                ElseIf terzoR = 3 Then
                    If CheckBox3.Checked Then
                        TextBox1.BackColor = Color.Green
                    End If
                Else
                    TextBox1.BackColor = Color.Black
                End If
            End If
        ElseIf secondoR = 2 Then

            If CheckBox2.Checked Then
                If terzoR = 2 Then
                    If CheckBox2.Checked Then
                        TextBox1.BackColor = Color.Green
                    End If
                ElseIf terzoR = 3 Then
                    If CheckBox3.Checked Then
                        TextBox1.BackColor = Color.Green
                    End If
                Else
                    TextBox1.BackColor = Color.Black
                End If
            End If
        End If
    ElseIf primoR = 2 Then
        'ecc...
    Else
        TextBox1.BackColor = Color.Black
    End If
    'checkbox10 = 0 , checkbox1 = 1, checkbox1 = 2

这里是complete code

2 个答案:

答案 0 :(得分:0)

您没有说这是什么语言,所以我不知道它是VBA(来自Office App),VB6还是VB.Net或其他。因此,这可能不是开箱即用的,但它应该为您提供一个起点。

  Green = False
  For primoR = 0 To 9
      If CType(Me.Controls("Checkbox" & IIf(primoR = 0, 10, primoR)), CheckboxBox).Checked Then
      For secondoR = 1 To 9
          If CType(Me.Controls("Checkbox" & IIf(secondoR = 0, 10, secondoR)), CheckboxBox).Checked Then
          For terzoR = 0 To 9
              If CType(Me.Controls("Checkbox" & IIf(terzoR = 0, 10, terzoR)), CheckboxBox).Checked Then
                  Green = True
              End If
          Next
      End If
  Next
  TextBox1.BackColor = IIf(Green, Color.Green, Color.Black)

答案 1 :(得分:0)

Private randomNumbers As New Random
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim randomNumber1 As Integer = randomNumbers.Next(0, 10)
    Dim randomNumber2 As Integer = randomNumbers.Next(0, 10)
    Dim randomNumber3 As Integer = randomNumbers.Next(0, 10)
    Label1.Text = (randomNumber1 + 1).ToString
    Label2.Text = (randomNumber2 + 1).ToString
    Label3.Text = (randomNumber3 + 1).ToString

    Dim checkA = CType(Me.Controls.Item($"CheckBox{randomNumber1 + 1}"), CheckBox)
    Dim checkB = CType(Me.Controls.Item($"CheckBox{randomNumber2 + 1}"), CheckBox)
    Dim checkC = CType(Me.Controls.Item($"CheckBox{randomNumber3 + 1}"), CheckBox)

    If checkA.Checked AndAlso checkB.Checked And checkC.Checked Then
        TextBox1.BackColor = Color.Green
    Else
        TextBox1.BackColor = Color.Black
    End If
End Sub