Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Questions (Windows Mobile)
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Questions (Windows Mobile) Post any question regarding Basic4ppc.

Click event during a for loop

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-12-2008, 10:32 AM
pmu5757's Avatar
Knows the basics
 
Join Date: Sep 2007
Location: Metz (France)
Posts: 67
Default Click event during a for loop

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.
Reply With Quote
  #2 (permalink)  
Old 04-12-2008, 10:55 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,733
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

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
Reply With Quote
  #3 (permalink)  
Old 04-12-2008, 11:47 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Quote:
Originally Posted by pmu5757 View Post
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.
Reply With Quote
  #4 (permalink)  
Old 04-12-2008, 12:07 PM
pmu5757's Avatar
Knows the basics
 
Join Date: Sep 2007
Location: Metz (France)
Posts: 67
Default 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
Reply With Quote
  #5 (permalink)  
Old 04-12-2008, 01:08 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

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
Reply With Quote
  #6 (permalink)  
Old 04-12-2008, 03:44 PM
pmu5757's Avatar
Knows the basics
 
Join Date: Sep 2007
Location: Metz (France)
Posts: 67
Default

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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
event doubleclick and click on the cell (table object)... micro Questions (Windows Mobile) 2 08-19-2008 05:50 PM
Unwanted Click Event Zenerdiode Questions (Windows Mobile) 6 05-06-2008 05:25 AM
TreeView Click Event RandomCoder Basic4ppc Wishlist 5 02-28-2008 03:23 PM
Do...Loop While Put Claude Questions (Windows Mobile) 1 07-24-2007 08:02 PM
Endless loop Cableguy Questions (Windows Mobile) 8 06-28-2007 04:36 PM


All times are GMT. The time now is 07:06 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0