Hi, I am a newcomer to Visual Studio 2005 and am having trouble making a timer which will work out the time it takes to download a file. The file is 45 kb and the user can change the speed with a scrollbar, which is called hsbSpeed, from 1 – 10 kbps. The timer will then work out the download time and count down to zero.

I was thinking along the line of this:

Counter = (45 \ hsbSpeed.Value) - 1
          txtCount.Text = Counter

but it only subtracts once and stops. I tried using a loop but that also didn’t work :

Counter = 45 \ hsbSpeed.Value
              Do While (Counter > 0)
                  Counter = Counter - 1
              Loop
              txtCount.Text = Counter

Any ideas on how to solve this?

Recommended Answers

All 3 Replies

I haven't done any VB coding myself, but this:

Do While (Counter > 0)
                  Counter = Counter - 1
              Loop
              txtCount.Text = Counter

Looks like it will keep iterating down until the variable Counter is equal to zero, and then it will set the Text value of txtCount to 0.
Try this:

Do While (Counter > 0)
                  Counter = Counter - 1
                  txtCount.Text = Counter
              Loop

You will probably also want to slow the loop down so that it only loops once per second. Perhaps pull the system time at the start of the loop and then wait at the end of the loop until the system time is equal to that time + 1.

Your file is 45kb and say your speed is set to 5kb. You are setting your counter to 45/5 every time. Set a variable called say "BytesLeft". After you have read 5kb subtract it from you "BytesLeft".
Try something like this:

Imports System.Threading

Public Class Form1
    Private BytesLeft As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        BytesLeft = 45
        Do While BytesLeft
            txtCount.Text = (BytesLeft \ hsbSpeed.Value) - 1
            txtCount.Refresh()
            Thread.Sleep(1000)
            BytesLeft -= hsbSpeed.Value
        Loop
    End Sub
End Class

Thanks for the help guys, I had tried something similar to the code that you gave me David but it didn't work. We have to explain our coding and show the lecturer how so the code that you kindly showed me Wayne was a bit too complicated. In the end I figured out a different way to do it but thanks all the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.