Reputation: 3235
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
Upvotes: 1
Views: 51
Reputation: 8868
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
Upvotes: 1
Reputation: 1371
This code finds all the information you need
I am still trying to implement the values in the respective button clicks events
Private Sub btnAdd_Click()
S1 = "132"
S2 = "321"
FindPlace
tbOne.Text = S1
End Sub
Private Sub FindPlace()
Dim Y As Integer
Dim i As Integer
tbOne.Text = S1
lbOne.Clear
For Y = 1 To Len(S2)
pos = Mid(S1, 4 - Y, 1)
S3 = Mid(S2, Y, 1)
For i = 1 To Len(S1)
If Mid(S1, i, 1) = S3 Then
lbOne.AddItem "Press " & i & " the value " & pos & " is in Position " & Mid(S1, i, 1)
End If
Next
Next
End Sub
Upvotes: 0