Results 1 to 6 of 6

Thread: delay loop

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2001
    Location
    Melbourne
    Posts
    36

    Question delay loop

    hey, im quite new to visual basic so i dont know much. i was wondering how do i make a delay loop, for example like a countdown, like i click a button and it displays 1 to 10, one second a number( like 10, then it waits a seccond, 9, waits another second and so on) and when it reaches 0 more text appears or a new form opens up and stuff like that.

    Thanx
    WoG_sTyLe_69

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    try this ...
    Start a new project,
    add a command button and paste the code below into the form.

    Code:
    Option Explicit
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Private Sub Command1_Click()
        Dim i As Integer
        For i = 10 To 1 Step -1
            Label1.Caption = i
            DoEvents
            Sleep 1000
        Next i
    End Sub
    -= a peet post =-

  3. #3
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    You can use a timer control for this purpose. Add one to ur form and set its interval property to 1000. Best to set timers as 'disabled' until u use. Enabled = False

    Timers are not quite as accurate as atomic clocks or even egg timers but they are good enuf for ur purpose.

    Form-------------------------
    Dim countdown as integer

    private sub command1_click
    countdown = 10
    timer1.enabled = true
    command1.enabled = false 'prevent double firing
    end sub

    private sub timer1_timer
    label1.caption = countdown
    countdown = countdown -1
    if countdown <= 0 then
    timer1.enabled = false 'good idea to turn timers off
    command1.enabled = true
    'Process other stuff u want to show
    end if
    end sub
    -----------------------------

    Regards
    Stuart
    http://www.gstsmartbook.com

  4. #4
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Sorry Pete, just wanted to add a word of warning for wog style on using sleep function. Would make no diff in this simple program but is not a good idea to tie up processor in more complicated programs.
    Regards
    Stuart
    http://www.gstsmartbook.com

  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    beachbum, no problem, I do agree.... I wasn't aware of the fact that it tied up the processor.. well I am now
    -= a peet post =-

  6. #6
    Hyperactive Member Jason Badon's Avatar
    Join Date
    Feb 2001
    Location
    Colorado
    Posts
    329
    or you could do this

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     Label1.Caption = 10
    4.     Timer1.Interval = 1000
    5.  
    6. End Sub
    7.  
    8.  
    9.  
    10. Private Sub Timer1_Timer()
    11.  
    12.     Label1.Caption = Label1.Caption - 1
    13.     Me.Refresh
    14.    
    15.     If Label1.Caption = 0 Then
    16.         Label1.Caption = "WOW"
    17.         Timer1.Enabled = False
    18.     End If
    19.  
    20. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width