PDA

View Full Version : [SOLVED] Textbox Loop



icemail
06-17-2016, 06:51 AM
hi

im making little game. about skills and hotkeys. Picture coming and press short keys.

create userform1
start and textbox

press start button timer is starting
and findin a key

after questin for loop how many times press a key(5-10-15-20-50-100 whatever)

and there is my questin. i dont trigger for loop. key is always same not change :(

pls help me
thx


my codes



Dim n As Integer
Dim shortkeys As Variant
Dim correct, incorrect, sum As String
Dim StartTime As Double
Dim val As String


Private Sub CommandButton1_Click()

StartTime = Timer

Call keys

End Sub


Sub keys()

shortkeys = Array("m", "a", "g", "b", "h", "p", "y", "o", "s", "g", "r", "u", "l", "u", "u", "d", "e", "c", "f", "d", "f", "k", "p", "f", "h", "d", "m", "f", "p", "c", "w", "r", "s", "w", "o", "i", "b", "t", "c", "e", "g", "g", "s", "s", "s", "g", "g", "g", "p", "p", "p", "a", "a", "a", "m", "m", "m", "b", "l", "l", "d", "a", "t", "t", "t", "t", "l", "m", "r", "h", "g", "b", "c", "s", "c", "a", "w", "r", "b", "s", "u", "f", "g", "c", "g", "t", "n", "r", "v", "l", "u", "b", "n", "f", "b", "x", "a", "b", "w", "r", "t", "m", "t", "c", "b", "v", "l", "t", "d", "v", "r")

n = Application.WorksheetFunction.RandBetween(LBound(shortkeys), UBound(shortkeys))

'Show keys
Label7.Caption = shortkeys(n)

val = InputBox("Enter repeat number")

End Sub


Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

For i = 1 To val


If TextBox1.Text = shortkeys(n) Then

correct = correct + 1

Else

incorrect = incorrect + 1

End If


TextBox1.Text = ""
sum = correct + incorrect
percentage = correct / sum

Label6.Caption = Format(percentage, "Percent")
Label4.Caption = correct

Label1.Caption = Format((Timer - StartTime) / 86400, "hh:mm:ss")


Next i


End Sub

mdmackillop
06-17-2016, 07:56 AM
Initialize routine added

Option Explicit
Dim n As Integer
Dim shortkeys As Variant
Dim correct, incorrect, sum As String
Dim StartTime As Double
Dim val As String

Private Sub CommandButton1_Click()
StartTime = Timer
Call keys
End Sub


Sub keys()
shortkeys = Array("m", "a", "g", "b", "h", "p", "y", "o", "s", "g", "r", "u", "l", "u", "u", "d", "e", "c", "f", "d", "f", "k", "p", "f", "h", "d", "m", "f", "p", "c", "w", "r", "s", "w", "o", "i", "b", "t", "c", "e", "g", "g", "s", "s", "s", "g", "g", "g", "p", "p", "p", "a", "a", "a", "m", "m", "m", "b", "l", "l", "d", "a", "t", "t", "t", "t", "l", "m", "r", "h", "g", "b", "c", "s", "c", "a", "w", "r", "b", "s", "u", "f", "g", "c", "g", "t", "n", "r", "v", "l", "u", "b", "n", "f", "b", "x", "a", "b", "w", "r", "t", "m", "t", "c", "b", "v", "l", "t", "d", "v", "r")
n = Application.WorksheetFunction.RandBetween(LBound(shortkeys), UBound(shortkeys))
'Show keys
Label7.Caption = shortkeys(n)

End Sub
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim i As Long, percentage As Single
For i = 1 To val
If TextBox1.Text = shortkeys(n) Then
correct = correct + 1
Else
incorrect = incorrect + 1
End If
TextBox1.Text = ""
sum = correct + incorrect
percentage = correct / sum
Label6.Caption = Format(percentage, "Percent")
Label4.Caption = correct
Label1.Caption = Format((Timer - StartTime) / 86400, "hh:mm:ss")
Next i
End Sub


Private Sub UserForm_Initialize()
val = InputBox("Enter repeat number")
keys
End Sub

icemail
06-18-2016, 12:55 AM
Thx but loop is dont trigger and always same key :(

mdmackillop
06-18-2016, 02:56 AM
I don't understand the rules of the game.

snb
06-18-2016, 05:04 AM
Label1.Caption = Mid("magbhpyosgruluudecfdfkpfhdmfpcwrswoibtceggsssgggpppaaammmblldattttlmrhgbcsc awrbsufgcgtnrvlubnfbxabwrtmtcbvltdvr", Rnd * 110, 1)

icemail
06-21-2016, 12:50 AM
i change my code
game rule is label7 or shortkeys(n) must be same with textbox1.text (If TextBox1.Text = shortkeys(n) Then)

but im so confused. My loop is dont trigger. and score is same .



Option Explicit
Dim n As Integer
Dim shortkeys As Variant
Dim correct, incorrect, sum As String
Dim StartTime As Double
Dim val As String
Sub keys()
shortkeys = Array("m", "a", "g", "b", "h", "p", "y", "o", "s", "g", "r", "u", "l", "u", "u", "d", "e", "c", "f", "d", "f", "k", "p", "f", "h", "d", "m", "f", "p", "c", "w", "r", "s", "w", "o", "i", "b", "t", "c", "e", "g", "g", "s", "s", "s", "g", "g", "g", "p", "p", "p", "a", "a", "a", "m", "m", "m", "b", "l", "l", "d", "a", "t", "t", "t", "t", "l", "m", "r", "h", "g", "b", "c", "s", "c", "a", "w", "r", "b", "s", "u", "f", "g", "c", "g", "t", "n", "r", "v", "l", "u", "b", "n", "f", "b", "x", "a", "b", "w", "r", "t", "m", "t", "c", "b", "v", "l", "t", "d", "v", "r")
n = Application.WorksheetFunction.RandBetween(LBound(shortkeys), UBound(shortkeys))
'Show keys
Label7.Caption = shortkeys(n)

End Sub

Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim i As Long, percentage As Single

For i = 1 To val
Call keys

If TextBox1.Text = shortkeys(n) Then
correct = correct + 1
Else
incorrect = incorrect + 1
End If
TextBox1.Text = ""
sum = correct + incorrect

Label4.Caption = correct
percentage = correct / sum
Label6.Caption = Format(percentage, "Percent")
Label1.Caption = Format((Timer - StartTime) / 86400, "hh:mm:ss")
Next i

End Sub


Private Sub UserForm_Initialize()
val = InputBox("Enter repeat number")
StartTime = Timer

End Sub

mdmackillop
06-21-2016, 05:43 AM
Still don't know what you're trying to achieve. Best guess


Option Explicit
Dim n As Integer
Dim shortkeys As Variant
Dim correct, incorrect, sum As String
Dim StartTime As Double
Dim val As Long, v As Long
Sub keys()
shortkeys = Array("m", "a", "g", "b", "h", "p", "y", "o", "s", "g", "r", "u", "l", "u", "u", "d", "e", "c", "f", "d", "f", "k", "p", "f", "h", "d", "m", "f", "p", "c", "w", "r", "s", "w", "o", "i", "b", "t", "c", "e", "g", "g", "s", "s", "s", "g", "g", "g", "p", "p", "p", "a", "a", "a", "m", "m", "m", "b", "l", "l", "d", "a", "t", "t", "t", "t", "l", "m", "r", "h", "g", "b", "c", "s", "c", "a", "w", "r", "b", "s", "u", "f", "g", "c", "g", "t", "n", "r", "v", "l", "u", "b", "n", "f", "b", "x", "a", "b", "w", "r", "t", "m", "t", "c", "b", "v", "l", "t", "d", "v", "r")
n = Application.WorksheetFunction.RandBetween(LBound(shortkeys), UBound(shortkeys))
'Show keys
Label7.Caption = shortkeys(n)
End Sub

Private Sub TextBox1_Change()
Dim i As Long, percentage As Single
v = v + 1
If v > val Then
MsgBox "No more attempts"
Exit Sub
End If

If TextBox1.Text = shortkeys(n) Then
correct = correct + 1
Else
incorrect = incorrect + 1
End If

With Me.TextBox1
.SelStart = 0
.SelLength = Len(.Text)
End With
Call keys
sum = correct + incorrect
Label4.Caption = correct
percentage = correct / sum
Label6.Caption = Format(percentage, "Percent")
Label1.Caption = Format((Timer - StartTime) / 86400, "hh:mm:ss")
End Sub

Private Sub UserForm_Initialize()
val = CLng(InputBox("Enter repeat number"))
StartTime = Timer
Call keys
End Sub