Click event during a for loop

Discussion in 'Questions (Windows Mobile)' started by pmu5757, Apr 12, 2008.

  1. pmu5757

    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.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    You should not use a loop here.
    I would have store the current number index in a global variable.
    Something like:
    Code:
    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
     
  3. agraham

    agraham Expert Licensed User

    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.
     
  4. pmu5757

    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
     
  5. agraham

    agraham Expert Licensed User

    Something like this?
    Code:
    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
     
  6. pmu5757

    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
     
Loading...