Basic4ppc - Windows Mobile Development  

Go Back   Basic4ppc - Windows Mobile Development > Main Category > Questions & Help Needed
Home Register FAQ Members List Search Today's Posts Mark Forums Read

Questions & Help Needed Post any question regarding Basic4ppc.


Click event during a for loop


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-12-2008, 11:32 AM
pmu5757's Avatar
Knows the basics
 
Join Date: Sep 2007
Location: Metz (France)
Posts: 53
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, 11:51 AM
Senior Member
 
Join Date: Mar 2008
Posts: 114
Default

I think what you are looking for is "InputBox" functionality...?

How about using a counter....?
Quote:
Sub globals
...Dim MyAnswers(20)
...Counter = 0
end sub

sub ButOK_Click
...Counter = Counter + 1
...MyAnswers(Counter) = Text1.Text
...if Counter = 20 then DoMyResults
end sub
Alternatively, instead of the second FOR, why not utilise Do While...until... (Counter = 20)?

Sorry cant really answer more unless you post an example of what you have so far so I can understand better.

Last edited by badkarma : 04-12-2008 at 11:54 AM.
Reply With Quote
  #3 (permalink)  
Old 04-12-2008, 11:55 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 3,334
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
  #4 (permalink)  
Old 04-12-2008, 11:58 AM
Senior Member
 
Join Date: Mar 2008
Posts: 114
Default

I won this time! But, Erel, yours is more informative ...

Deja Vu ?!?!?!
Reply With Quote
  #5 (permalink)  
Old 04-12-2008, 12:47 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,896
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
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
  #6 (permalink)  
Old 04-12-2008, 01:07 PM
pmu5757's Avatar
Knows the basics
 
Join Date: Sep 2007
Location: Metz (France)
Posts: 53
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
  #7 (permalink)  
Old 04-12-2008, 02:08 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,896
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
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
  #8 (permalink)  
Old 04-12-2008, 04:44 PM
pmu5757's Avatar
Knows the basics
 
Join Date: Sep 2007
Location: Metz (France)
Posts: 53
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 On
Pingbacks are On
Refbacks are On

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


All times are GMT. The time now is 01:12 PM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0