By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,750 Members | 1,471 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,750 IT Pros & Developers. It's quick & easy.

Looping character or number visual basic 6

P: 12
Hi all,
Sorry im beginer in vb.
I want making programm looping character or number.
Just say i have numbers from 100 to 10000.

just sample:

Private Sub Timer1_Timer()

if check1.value= 1 then
for i = 100 to 10000
text1.text= i
next i
end if

end sub

but this looping not like i wanted,

I want all number until the end will be display on text1.text, not just the last number 10000. We can see what the value on text1.text when looping running.
If value of i = 10000 then repeat again from the begining until check box value=0,

when click check box the looping stop, the result text1 can be = 100 or 500 or 300 or 9000 or etc..

any suggestion or code script else?...
please help..

thanks in advance
Jul 12 '07 #1
Share this Question
Share on Google+
20 Replies


hariharanmca
100+
P: 1,977
Hi all,
Sorry im beginer in vb.
I want making programm looping character or number.
Just say i have numbers from 100 to 10000.

just sample:

Private Sub Timer1_Timer()

if check1.value= 1 then
for i = 100 to 10000
text1.text= i
next i
end if

end sub

but this looping not like i wanted,

I want all number until the end will be display on text1.text, not just the last number 10000. We can see what the value on text1.text when looping running.
If value of i = 10000 then repeat again from the begining until check box value=0,

when click check box the looping stop, the result text1 can be = 100 or 500 or 300 or 9000 or etc..

any suggestion or code script else?...
please help..

thanks in advance

So u can use


Expand|Select|Wrap|Line Numbers
  1. Private Sub Timer1_Timer()
  2. text1.text = ""
  3. if check1.value= 1 then
  4. for i = 100 to 10000
  5. text1.text = text1.text & "  " & i
  6. next i
  7. end if
  8.  
  9. end sub
this will help u
Jul 12 '07 #2

Expert 5K+
P: 8,434
Actually Ifoel, I think your original code will work, if you put a DoEvents statement inside the loop. This allows Windows to take time out to do other things, like updating the display.

(You might also need to Refresh the form, but I don't think so.)
Jul 12 '07 #3

P: 12
Thanks alot Killer42 and Hariharanmca...........
this very help full
.....
Jul 13 '07 #4

hariharanmca
100+
P: 1,977
So u can use


Expand|Select|Wrap|Line Numbers
  1. Private Sub Timer1_Timer()
  2. text1.text = ""
  3. if check1.value= 1 then
  4. for i = 100 to 10000
  5. text1.text = text1.text & "  " & i
  6. next i
  7. end if
  8.  
  9. end sub
this will help u


I don't have VB in my system so i cannot test tat so i'm giving hint tatsall....
Jul 13 '07 #5

P: 12
Hi Hariharanmca
thanks for u reply...
My original code has corect, like Killer42 said. I just add DoEvents in loop.
now i get trouble, when i click check box for stop looping, why looping still running. and value of Text1.text always 10000.
i have put code when value check1 box = 0, timer1.enable=false.
why this haven?. how to directly stop looping when timer1 false?
so i can get value on text1.text like i wish..
i mean when looping running and on value text1.text box=500 when i press check1 box to stop looping should be value text1.text=500 not 10000.

any idea or script else?
thanks in advance
Jul 13 '07 #6

hariharanmca
100+
P: 1,977
Hi Hariharanmca
thanks for u reply...
My original code has corect, like Killer42 said. I just add DoEvents in loop.
now i get trouble, when i click check box for stop looping, why looping still running. and value of Text1.text always 10000.
i have put code when value check1 box = 0, timer1.enable=false.
why this haven?. how to directly stop looping when timer1 false?
so i can get value on text1.text like i wish..
i mean when looping running and on value text1.text box=500 when i press check1 box to stop looping should be value text1.text=500 not 10000.

any idea or script else?
thanks in advance

Can u explain your exact requirement....so tat I can give some other suggestion? Because you cannot stop when timer enabled is false. Because itís just like another thread
Jul 13 '07 #7

P: 12
Ok thanks Hariharanmca...
I just want make programm like LOTTRE do u know LOTTRE.. ..mmm to defficult to say...cos my english so poor...

I just 1 winning or 1 value in text1.text from looping run, not all number from 100 to 10000 display on text1.tex.

when i click check box value= 1 then looping on, If check1 box = 0 looping off

this sample:

Private Sub Check1_Click()

If Check1.Value = 1 Then
Timer1.Enabled = True
ElseIf check1.value=0 then
Timer1.Enabled = False
End If

End Sub

Private Sub Form_Load()
timer1.interval=10
End sub

Private Sub Timer1_Timer()
For j = 100 To 10000
DoEvents
Text1.Text = j
Next j
End Sub

this code has correct, the problem is, when looping run
and on text1.text=500 shown i click check1 box to stop looping . and i want the winning is number 500, but value of text1.text always shown 10000. cos looping still run event i click check1 box to put value=0. whay this haven Hariharanmca?
do u have sugesstion?
Jul 13 '07 #8

hariharanmca
100+
P: 1,977
Ok thanks Hariharanmca...
I just want make programm like LOTRE do u know LOTRE.. ..mmm to defficult to say...cos my english so poor...

I just 1 winning or 1 value in text1.text when looping run, not all number from 100 to 10000 display on text1.tex.

when i click check box value= 1 then looping on, If check1 box = 0 stop looping

this sample:

Private Sub Check1_Click()

If Check1.Value = 1 Then
Timer1.Enabled = True
ElseIf check1.value=0 then
Timer1.Enabled = False
End If

End Sub

Private Sub Form_Load()
timer1.interval=10
End sub

Private Sub Timer1_Timer()
For j = 100 To 10000
DoEvents
Text1.Text = j
Next j
End Sub

this code has correct, the problem is, when looping run
and on text1.text=500 shown i click check1 box to stop looping . and i want the winning is number 500, but value of text1.text always shown 10000. cos looping still run event i click check1 box to put value=0. whay this haven Hariharanmca?
do u have sugesstion?



1 Enable the timer to true in Properties
2.Set the timer interval to 10 or 20 for random speed

take the code below





Expand|Select|Wrap|Line Numbers
  1. Private Sub Timer1_Timer()
  2.     If Check1.Value = 1 Then
  3.         For j = 100 To 10000
  4.             DoEvents
  5.             Text1.Text = j
  6.         Next j
  7.     End if
  8. End Sub

(No VB in my system. Code not tested)
Jul 13 '07 #9

Expert 5K+
P: 8,434
I think perhaps you have misunderstood how the timer works. When the timer fires, it will start running the code in your Timer event procedure. From that point on, the time is irrelevant. It just works through the code until it finishes, or gets kicked out of the routine or whatever. So, in this case your code effectively says "if the check box is checked, then go and do all of this". If you want it to do something different along the way, based on the checkbox value, you have to code it that way.

The secret to all this is to learn to think like a computer. In other words, stupid!

I'm serious - computers are completely stupid. They don't do what you want them to do. They do precisely what they are told to do, one step at a time. Where people always go wrong is in trying to think sensibly about their code. Computers are not sensible - they just follow instructions. To understand what your code will do, you have to follow it statement by statement, without jumping to conclusions and making assumptions.

Your code says to count from 100 to 10,000 so that's what it does. There's nothing in there that says to stop, for any reason.

If you want it to stop when you "turn off" the checkbox, I'd say there are two basic approaches you could take:
  1. Do one step each time the timer control fires, if the checkbox is checked.
  2. Do it all in one go as you have in your original post, but add some code inside the loop to exit if the checkbox is not set. This would be the simpler method to implement, since it's only a small change to your existing code.
Jul 13 '07 #10

Expert 5K+
P: 8,434
1 Enable the timer to true in Properties
2.Set the timer interval to 10 or 20 for random speed
take the code below
Expand|Select|Wrap|Line Numbers
  1. Private Sub Timer1_Timer()
  2.     If Check1.Value = 1 Then
  3.         For j = 100 To 10000
  4.             DoEvents
  5.             Text1.Text = j
  6.         Next j
  7.     End if
  8. End Sub
This still suffers from the original problem, which is that you are saying "if the checkbox is checked, do everything. Otherwise, do nothing". What you need to do is stop when the checkbox value changes. See my prior post. (Sorry, it took quite a while to type.)
Jul 13 '07 #11

P: 12
Yes Killer42...u r correct..
i have put code like
still not work

Expand|Select|Wrap|Line Numbers
  1. Private Sub Timer1_Timer()
  2.     If Check1.Value = 1 Then
  3.         For j = 100 To 10000
  4.             DoEvents
  5.             Text1.Text = j
  6.         Next j
  7.     End if
  8. End Sub
so whould u give me some code Killer42
to stop loop when i checked=0.

thanks in advance
Jul 13 '07 #12

hariharanmca
100+
P: 1,977
This still suffers from the original problem, which is that you are saying "if the checkbox is checked, do everything. Otherwise, do nothing". What you need to do is stop when the checkbox value changes. See my prior post. (Sorry, it took quite a while to type.)
Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim intDisplay as integer
  3.  
  4. Private Sub Timer1_Timer()
  5.     If Check1.Value = 1 Then
  6.         if intDisplay > 10000 then intDisplay = 99
  7.         intDisplay = intDisplay + 1
  8.         Text1.Text = intDisplay
  9.     End if
  10. End Sub
  11.  
  12. Private Sub Check1_Click()
  13.     intDisplay = 100
  14. End Sub
(Not Tested)

i think this will work
Jul 13 '07 #13

hariharanmca
100+
P: 1,977
Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim intDisplay as integer
  3.  
  4. Private Sub Timer1_Timer()
  5.     If Check1.Value = 1 Then
  6.         if intDisplay > 10000 then intDisplay = 99
  7.         intDisplay = intDisplay + 1
  8.  
  9.         Text1.Text = intDisplay
  10.     End if
  11. End Sub
  12.  
  13. Private Sub Check1_Click()
  14.     intDisplay = 100
  15. End Sub
(Not Tested)

i think this will work

my request, i want to know the out put or this code. can any 1 test it and reply

(I don't have VB in my system).
Jul 13 '07 #14

Expert 5K+
P: 8,434
Expand|Select|Wrap|Line Numbers
  1. Private Sub Timer1_Timer()
  2.     If Check1.Value = 1 Then
  3.         For j = 100 To 10000
  4.             DoEvents         ' You should swap this line...
  5.             Text1.Text = j   ' ...and this line.
  6.         Next j
  7.     End if
  8. End Sub
so whould u give me some code Killer42
to stop loop when i checked=0.
Nope. :)

But I'll tell you how you can do it. First of all, I'd recommend reversing the order of the DoEvents and the Text1.Text = j - but that's unrelated.

Inside your loop, just code "if checkbox is not checked, escape from the loop". I'd prefer you do the translation from English to VB. It's fairly straightforward. Hint, you want to Exit from the For loop.
Jul 13 '07 #15

hariharanmca
100+
P: 1,977
Nope. :)

But I'll tell you how you can do it. First of all, I'd recommend reversing the order of the DoEvents and the Text1.Text = j - but that's unrelated.

Inside your loop, just code "if checkbox is not checked, escape from the loop". I'd prefer you do the translation from English to VB. It's fairly straightforward. Hint, you want to Exit from the For loop.

Expand|Select|Wrap|Line Numbers
  1. Dim intDisplay as integer
  2.  
  3. Private Sub Timer1_Timer()
  4.     If Check1.Value = 1 Then
  5.         if intDisplay > 10000 then intDisplay = 99
  6.         intDisplay = intDisplay + 1
  7.         Text1.Text = intDisplay
  8.     End if
  9. End Sub
  10.  
  11. Private Sub Check1_Click()
  12.     intDisplay = 100
  13. End Sub
how about this code
Jul 13 '07 #16

Expert 5K+
P: 8,434
how about this code
Yes, that looks like the other option I mentioned. Incrementing each time the timer event fires. For this you would want a pretty small timer interval.
Jul 13 '07 #17

hariharanmca
100+
P: 1,977
Yes, that looks like the other option I mentioned. Incrementing each time the timer event fires. For this you would want a pretty small timer interval.
Yha, Whatever I mention above for properties.

is that code working fine?
Jul 13 '07 #18

Expert 5K+
P: 8,434
I haven't had a chance to try it. But it looks alright.
Jul 13 '07 #19

P: 12
sorry hariharanmca..
not work
when looping run the resul text1.text =1
just it... and stop and looping not run again even check1 value =1
Jul 13 '07 #20

hariharanmca
100+
P: 1,977
sorry hariharanmca..
not work
when looping run the resul text1.text =1
just it... and stop and looping not run again even check1 value =1
Expand|Select|Wrap|Line Numbers
  1. Dim intDisplay as integer
  2.  
  3. Private Sub Timer1_Timer()
  4.     If Check1.Value = 1 Then
  5.         if intDisplay > 10000 then intDisplay = 99
  6.         intDisplay = intDisplay + 1
  7.         Text1.Text = intDisplay
  8.     End if
  9. End Sub
  10.  
  11. Private Sub Check1_Click()
  12.     intDisplay = 100
  13. End Sub

Try this qode
Jul 13 '07 #21

Post your reply

Sign in to post your reply or Sign up for a free account.