Click event during a for loop

pmu5757

Member
Licensed User
Hello everybody,
I'm making a little program where the user must remember numbers (for example 20 numbers between 1 an 100).
I've done a first "for" loop to print the 20 numbers to remember.
The I make a second "for" loop, where the user must fill in a textbox with his answer for each number he tries. When the user has filled the textbox, he clicks on a "ok" button.
My problem is that the sub "buttonok_click" is not the same sub as the sub where is the loop.
How can I do in the loop to wait for the ok click without blocking completely the program ?

Thank you for your help.

Pascal.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should not use a loop here.
I would have store the current number index in a global variable.
Something like:
B4X:
Sub Globals
    'Declare the global variables here.
    currentNumber = 0
    Dim numbers(20)
    
End Sub

Sub App_Start
    Form1.Show
    For i = 0 To 19
        number(i) = Rnd(1,101)
    Next
    'Show the numbers...
End Sub


Sub Button1_Click
    If textbox1.text = number(currentNumber) Then
        'correct answer
        currentNumber = currentNumber + 1
    Else
        'wrong answer
    End If
    If currentNumber = 20 Then
        'Finish
    End If
End Sub
 

agraham

Expert
Licensed User
Longtime User
How can I do in the loop to wait for the ok click without blocking completely the program ?
This is a very common misconception that people have who are new to event driven programming. Unlike a "traditional" program event driven programs don't need to wait in loops for something to happen, the Operating System takes care of this for you. You just need to write subroutines (event procedures) that are invoked when something (an event) happens. Your code deals with that, the subroutine exits and the Operating System will call your code when the next event happens. The most common events are probably caused by user input but a timer expiring or external data data arriving can also be the source of events.

Your code should never loop unnecessarily as it eats up processor time that another app might be able to use, and on portable devices it wastes power and shortens battery life.
 

pmu5757

Member
Licensed User
Click event during a loop

Hello,
Thank you for your firsts answers, but my problem is in fact a little bit more complicated than I first said.
I don't know if I can do my program without a loop...
In fact the number of numbers that the user must remember increase each time.

Here is my actual code :

Sub Bmemoirenouveau_Click
score=0
For i=1 To 99
memoire(i)=Rnd(0,100)
Next
For i=1 To 99
For j=1 To i
Msgbox("nombre à deviner n° " & j & ":" & memoire(j))
Next

For j=1 To i
Do Until bloque=0
Sleep(1000)
Loop
If TBmemoireessai.Text <> memoire(j) Then
perdu
End If
Next
score=score+1
Next

End Sub

Sub Bmemoireok_Click
bloque=0
End Sub

Sub perdu
Msgbox("vous avez retenu " & score & " nombres")
End Sub
 

agraham

Expert
Licensed User
Longtime User
Something like this?
B4X:
Sub Globals
    'Declare the global variables here.
    score = 0
    count = 1
    currentNumber = 0
    Dim numbers(100)
    
End Sub

Sub App_Start
    Form1.Show
    For i = 0 To 99
        numbers(i) = Rnd(1,101)
    Next
    Shownumbers
End Sub

Sub Shownumbers
    For i = 0 To count - 1
        s = s &" " & numbers(i)
    Next
    Msgbox(s, "Your score is " & score)
End Sub

Sub textbox1_KeyPress(key)
   If Asc(key) = 13 Then
     If textbox1.text = numbers(currentNumber) Then
        Msgbox("Correct!")
        currentNumber = currentNumber + 1
        score = score + 1
    Else
        'wrong answer
    End If
    textbox1.Text = ""
    If currentNumber = count Then
        count = count + 1
        currentNumber = 0         
        Shownumbers
     End If
  End If
End Sub
 

pmu5757

Member
Licensed User
Hello
Thank you Agraham, your code enabled me to do my job without loop, but only with counters.
It works very well !
Thank you also Badkarma and Erel.

Pascal
 
Top