Looping character or number visual basic 6

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ifoel
    New Member
    • Jul 2007
    • 12

    Looping character or number visual basic 6

    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
  • hariharanmca
    Top Contributor
    • Dec 2006
    • 1977

    #2
    Originally posted by Ifoel
    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


    Code:
    Private Sub Timer1_Timer()
    text1.text = ""
    if check1.value= 1 then
    for i = 100 to 10000
    text1.text = text1.text & "  " & i
    next i
    end if
    
    end sub
    this will help u

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      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.)

      Comment

      • Ifoel
        New Member
        • Jul 2007
        • 12

        #4
        thanks alot for all

        Thanks alot Killer42 and Hariharanmca... ........
        this very help full
        .....
        Last edited by Ifoel; Jul 13 '07, 02:28 AM. Reason: i want thanks to all participants

        Comment

        • hariharanmca
          Top Contributor
          • Dec 2006
          • 1977

          #5
          Originally posted by hariharanmca
          So u can use


          Code:
          Private Sub Timer1_Timer()
          text1.text = ""
          if check1.value= 1 then
          for i = 100 to 10000
          text1.text = text1.text & "  " & i
          next i
          end if
          
          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....

          Comment

          • Ifoel
            New Member
            • Jul 2007
            • 12

            #6
            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=f alse.
            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

            Comment

            • hariharanmca
              Top Contributor
              • Dec 2006
              • 1977

              #7
              Originally posted by Ifoel
              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=f alse.
              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

              Comment

              • Ifoel
                New Member
                • Jul 2007
                • 12

                #8
                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?

                Comment

                • hariharanmca
                  Top Contributor
                  • Dec 2006
                  • 1977

                  #9
                  Originally posted by Ifoel
                  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





                  Code:
                  Private Sub Timer1_Timer()
                  	If Check1.Value = 1 Then
                  		For j = 100 To 10000
                  			DoEvents
                  			Text1.Text = j
                  		Next j
                  	End if
                  End Sub

                  (No VB in my system. Code not tested)

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    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.

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by hariharanmca
                      1 Enable the timer to true in Properties
                      2.Set the timer interval to 10 or 20 for random speed
                      take the code below
                      Code:
                      Private Sub Timer1_Timer()
                      	If Check1.Value = 1 Then
                      		For j = 100 To 10000
                      			DoEvents
                      			Text1.Text = j
                      		Next j
                      	End if
                      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.)

                      Comment

                      • Ifoel
                        New Member
                        • Jul 2007
                        • 12

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

                        [CODE=vb]Private Sub Timer1_Timer()
                        If Check1.Value = 1 Then
                        For j = 100 To 10000
                        DoEvents
                        Text1.Text = j
                        Next j
                        End if
                        End Sub[/CODE]

                        so whould u give me some code Killer42
                        to stop loop when i checked=0.

                        thanks in advance

                        Comment

                        • hariharanmca
                          Top Contributor
                          • Dec 2006
                          • 1977

                          #13
                          Originally posted by Killer42
                          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.)
                          Code:
                          Dim intDisplay as integer
                          
                          Private Sub Timer1_Timer()
                          	If Check1.Value = 1 Then
                          		if intDisplay > 10000 then intDisplay = 99
                          		intDisplay = intDisplay + 1
                          		Text1.Text = intDisplay
                          	End if
                          End Sub
                          
                          Private Sub Check1_Click()
                          	intDisplay = 100
                          End Sub
                          (Not Tested)

                          i think this will work

                          Comment

                          • hariharanmca
                            Top Contributor
                            • Dec 2006
                            • 1977

                            #14
                            Originally posted by hariharanmca
                            Code:
                            Dim intDisplay as integer
                            
                            Private Sub Timer1_Timer()
                            	If Check1.Value = 1 Then
                            		if intDisplay > 10000 then intDisplay = 99
                            		intDisplay = intDisplay + 1
                            
                            		Text1.Text = intDisplay
                            	End if
                            End Sub
                            
                            Private Sub Check1_Click()
                            	intDisplay = 100
                            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).

                            Comment

                            • Killer42
                              Recognized Expert Expert
                              • Oct 2006
                              • 8429

                              #15
                              Originally posted by Ifoel
                              [CODE=vb]Private Sub Timer1_Timer()
                              If Check1.Value = 1 Then
                              For j = 100 To 10000
                              DoEvents ' You should swap this line...
                              Text1.Text = j ' ...and this line.
                              Next j
                              End if
                              End Sub[/CODE]

                              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.

                              Comment

                              Working...