Search code examples
vb6

Set order buttons can be clicked


I have THREE buttons on a form and would like to control the order in which they can be CLICKED based on an ever changing random number made up of 3 digits
I can control the click order as long as the random number does not change
As the code is now the random number is not being generated
I just change the random number to test
I understand what is needed is a valid test when clicking the buttons looked at using an Array not much luck

How to dynamically associate the position of the number in the String to control the buttons click order?

Private Sub btnAdd_Click()
  ck = 0
  GetPos
  tbOne.Text = S1
End Sub

Private Sub GetPos()
  Dim Y As Integer
  S1 = "132" 'Random Number

  For Y = 1 To Len(S1)
    pos = Mid(S1, Y, 1) 'Position of Value in S1 the Random Number
    lbOne.AddItem pos & vbNewLine
  Next
End Sub

Private Sub btnOne_Click()
  ck = ck + 1
  If Mid(S1, 1, 1) = "1" And ck = 3 Then
    btnOne.BackColor = vbYellow
  Else
  ck = 4

  If btnOne.BackColor = vbYellow Then
    Exit Sub
  End If
        
  btnOne.BackColor = vbRed

  If btnOne.BackColor <> vbYellow Then
    btnOne.Enabled = False
  End If
  End If
  End Sub

Private Sub btnTwo_Click()
  ck = ck + 1
  If Mid(S1, 2, 1) = "3" And ck = 1 Then
    btnTwo.BackColor = vbYellow
  Else
  ck = 4 ' Prevents other buttons from being Yellow

  If btnTwo.BackColor = vbYellow Then
    Exit Sub
  End If
   
  btnTwo.BackColor = vbRed
  If btnTwo.BackColor <> vbYellow Then
    btnTwo.Enabled = False
  End If
  End If
  End Sub

Private Sub btnThree_Click()
  ck = ck + 1
  If Mid(S1, 3, 1) = "2" And ck = 2 Then
    btnThree.BackColor = vbYellow
  Else
  ck = 4

  If btnThree.BackColor = vbYellow Then
    Exit Sub
  End If

  btnThree.BackColor = vbRed
  If btnThree.BackColor <> vbYellow Then
    btnThree.Enabled = False
  End If
  End If
  End Sub

Solution

  • This seems to do what you need:

    Private Sub btnOne_Click()
       ck = ck + 1
      
       If ck = Mid(S1, 1, 1) Then
          btnOne.BackColor = vbYellow
       Else
          btnOne.BackColor = vbRed
          btnOne.Enabled = False
       End If
    End Sub
    
    Private Sub btnTwo_Click()
       ck = ck + 1
      
       If ck = Mid(S1, 2, 1) Then
          btnTwo.BackColor = vbYellow
       Else
          btnTwo.BackColor = vbRed
          btnTwo.Enabled = False
       End If
    End Sub
    
    Private Sub btnThree_Click()
       ck = ck + 1
      
       If ck = Mid(S1, 3, 1) Then
          btnThree.BackColor = vbYellow
       Else
          btnThree.BackColor = vbRed
          btnThree.Enabled = False
       End If
    End Sub