# How to simplify a complex 4k lines if block?

• ### Question

•

I have tried case but isn't enjoy
maybe white "fore cycle or while"
i use visual basic 2016

I have 10 checkboxes, 3 random numbers from 0 to 9,<br/> If 3 checkboxes are white, the same 3 numbers as "randomly generated" are checked a textbox become
"the same number as 3 number"

this is an img of checkbox and textbox :
This is a huge simplification

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

Here is the complete code https://pastebin.com/h4ii6zbD

someone suggested this solution :

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)

but i have 10 checkbox in this case if i check any of the 10 checkbox it become green and don't do what i want only if 3 checkbox whit the same number generated earlier are checked then become green

• Edited by Friday, July 6, 2018 10:43 PM
Friday, July 6, 2018 10:33 PM

• As I understand it, you want the textbox to be green if the three random numbers match the checked checkboxes, so it could be that all three random numbers are the same, in which case only 1 checkbox actually matters, or two could be the same so only two checkboxes matter, or all three could be different and so three checkboxes matter.  If every checkbox is checked, the textbox will always be green because the three random numbers will always match.  Is that correct?

If so, you can use code like:

```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
```

Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

• Marked as answer by Sunday, July 8, 2018 2:01 AM
Saturday, July 7, 2018 2:09 PM

### All replies

• Hi

This is some test code to do what I *think* you are meaning.

Give it a try out to see if it is close to what you are asking for.

`' UNIQUE RANDOM INTEGER LISTOption Strict OnOption Explicit OnPublic Class Form1  Dim r As New Random  ' any number checkboxes  Dim howmanycheckboxes As Integer = 10  ' checkbox column size  Dim cbcs As Integer = 5  Dim WithEvents b As New Button  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load    Dim x As Integer = 10    Dim y As Integer = 40    For i As Integer = 1 To howmanycheckboxes      Dim ncb As New CheckBox      With ncb        .Width = 70        .Text = i.ToString        .Location = New Point(x, y)        y += 24        If y > cbcs * 24 + 40 - 1 Then          y = 40          x += 80        End If      End With      Controls.Add(ncb)    Next    With b      .Location = New Point(10, 10)      .Text = "GO"      AddHandler .Click, AddressOf b_Click    End With    Controls.Add(b)  End Sub  Private Sub b_Click(sender As Object, e As EventArgs)    checkboxes(Enumerable.Range(1, howmanycheckboxes).OrderBy(Function(n) r.Next).Take(3).ToList)  End Sub  Sub checkboxes(lst As List(Of Integer))    For Each c As Control In Controls      If c.GetType Is GetType(CheckBox) Then        Dim cb As CheckBox = DirectCast(c, CheckBox)        If lst.Contains(CInt(c.Text)) Then          cb.Checked = True          cb.BackColor = Color.LightGreen        Else          cb.Checked = False          cb.BackColor = Color.White        End If      End If    Next  End Sub  ' this sub is just for a DEMO  ' remove the whole sub and use  ' the Buttonmfor manual test.  Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown    Do      b.PerformClick()      Application.DoEvents()      Threading.Thread.Sleep(300)    Loop  End SubEnd Class`

Regards Les, Livingston, Scotland

• Edited by Saturday, July 7, 2018 2:15 AM amended for any number checkboxes
Saturday, July 7, 2018 12:45 AM
• Les,

I am still trying to figure where the random numbers and the text box comes in.

What is the purpose of the application? Is it a number picking game or what?

How can you have a 4000 line if statement? That seems like a problem. Do you have 400 checkboxes total?

If you can better explain what you want to do maybe there are other ways?

For example how does the random you mention get picked and how does the text box figure in? Maybe a picture will help.

What does this line of code do?

If CType(Me.Controls("Checkbox" & IIf(secondoR = 0, 10, secondoR)), CheckboxBox).Checked Then

Saturday, July 7, 2018 11:54 AM
• As I understand it, you want the textbox to be green if the three random numbers match the checked checkboxes, so it could be that all three random numbers are the same, in which case only 1 checkbox actually matters, or two could be the same so only two checkboxes matter, or all three could be different and so three checkboxes matter.  If every checkbox is checked, the textbox will always be green because the three random numbers will always match.  Is that correct?

If so, you can use code like:

```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
```

Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

• Marked as answer by Sunday, July 8, 2018 2:01 AM
Saturday, July 7, 2018 2:09 PM
•  If CType(Me.Controls("Checkbox" & IIf(secondoR = 0, 10, secondoR)), CheckboxBox).Checked Then

this line check for checked checkbox

then

its a simple 3 number piking game 3 number are generate randomly at the beginning then

a text box become green when 3 checkbox with the same number as randomly generate is piked its like a pin code keybord bat whit a chekbox

Sunday, July 8, 2018 12:35 AM