Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

why when i add time lag to my program in the code ->

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       TextBox1.Text = ""
       TextBox1.Text = "5"
     
   End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       System.Threading.Thread.Sleep(1000)
       TextBox1.Text = "4"
       System.Threading.Thread.Sleep(1000)
       TextBox1.Text = "3"
       System.Threading.Thread.Sleep(1000)
       TextBox1.Text = "2"
       System.Threading.Thread.Sleep(1000)
       TextBox1.Text = "1"
       System.Threading.Thread.Sleep(1000)
       TextBox1.Text = "Finished"
   End Sub

it does modify the textbox1.text to 4/3/2/1 it just waits 5 seconds and puts finished

what do i change?

  • *Experts*
Posted

The texbox doesnt have time to print the text becuase it goes to sleep right away so you need to call DoEvents before every sleep so it can finish:

textbox1.Text = "5"
Application.DoEvents()
System.Threading.Thread.Sleep(1000)
textbox1.Text = "4"
Application.DoEvents()
System.Threading.Thread.Sleep(1000)
textbox1.Text = "3"
Application.DoEvents()
System.Threading.Thread.Sleep(1000)
textbox1.Text = "2"
Application.DoEvents()
System.Threading.Thread.Sleep(1000)
textbox1.Text = "1"
Application.DoEvents()
System.Threading.Thread.Sleep(1000)
textbox1.Text = "DONE"

 

Also I suggest that you use timer for this kind of thing, it doesnt require you to put the thread to sleep so your application can respond during that time.

  • Leaders
Posted

you could do this :

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim i As Integer = 5
       Dim x As Integer

       For x = 1 To 5
           TextBox1.Text = i
           i = i - 1
           Application.DoEvents()
           Threading.Thread.Sleep(1000)
       Next

   End Sub

 

or better still as Divil says use a timer , eg :

Dim x As Integer = 5
' /// made available to all subs ^^^
' ///
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

       Dim i As Integer
       TextBox1.Text = x
       For i = 1 To 4
           With Timer1
               .Interval = 1000
               .Enabled = True
               .Start()
           End With
       Next

   End Sub

   Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

       TextBox1.Text = x
       x = x - 1
       If x < 1 Then
           Timer1.Stop()
           x = 5
       End If
   End Sub

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...