2

I am initiating the Hours, Minutes and Seconds

And then Converting it to total milliseconds.

And then what i am doing is subtracting the Total MilliSeconds from Elapsed MilliSeconds

i.e elms = cms - e.Milliseconds

(Evaluated Milliseconds = calculated Millisec. - Stopwatch.elapsedMilliseconds)

and then converting the Evaluated Milliseconds back to the HH:MM:SS:MS format.

but its not working due to some logical error, Thats why i need some assistance. please help me a little Here's My Code:

        Dim dd As Integer = 0
        Dim mm As Integer = 0
        Dim hh As Integer = 0
        Dim ss As Integer = 0
        Dim ms As Integer = 0
        Dim cms As Long = 0
        Dim elms As Long = 0

    Dim stopwatch As System.Diagnostics.Stopwatch = New System.Diagnostics.Stopwatch
    Dim dt As DispatcherTimer = New DispatcherTimer

    Private Sub StartButton_Click(sender As Object, e As RoutedEventArgs) Handles StartButton.Click

                hh = Hours.Text * 60 * 60 * 1000
                mm = Minutes.Text * 60 * 1000
                ss = Seconds.Text * 1000
                cms = hh + mm + ss
                hh = mm = ss = 0
                Start()
    End Sub

    Private Sub Start()

                dt.Interval = New TimeSpan(0, 0, 0, 0, 10)
                AddHandler dt.Tick, AddressOf ontimertick
                stopwatch.Start()
                dt.Start()

            End If
    End Sub

    Private Sub ontimertick()


            Dim e As New TimeSpan
            e = stopwatch.Elapsed

                elms = cms - e.Milliseconds
                ss = ((elms Mod (1000 * 60 * 60)) Mod (1000 * 60)) \ 1000
                mm = (elms Mod (1000 * 60 * 60)) \ (1000 * 60)
                hh = elms \ (1000 * 60 * 60)
                elms = elms Mod 1000

                MicroSeconds.Text = elms.ToString("00")
                Seconds.Text = ss.ToString("00")
                Minutes.Text = mm.ToString("00")
                Hours.Text = hh.ToString("00")
    End Sub
Saurabh Sharma
  • 2,422
  • 4
  • 20
  • 41

2 Answers2

0

The error is here :

ss = ((elms Mod (1000 * 60 * 60)) Mod (1000 * 60)) \ 1000
mm = (elms Mod (1000 * 60 * 60)) \ (1000 * 60)
hh = elms \ (1000 * 60 * 60)
elms = elms Mod 1000

it must be:

ss = ((elms Mod (1000 * 60 * 60)) Mod (1000 * 60)) \ 1000
 elms=elms-(ss*1000)
mm = (elms Mod (1000 * 60 * 60)) \ (1000 * 60)
 elms=elms-(mm*1000)
hh = elms \ (1000 * 60 * 60)
 elms=elms-(hh*1000)
elms = elms Mod 1000

OK?

Searush
  • 628
  • 1
  • 5
  • 18
0

You're over complicating it. All you need to do is store the total time as a TimeSpan object. Then, you can subtract the StopWatch.Elapsed time from it because it is also a TimeSpan. Then, the TimeSpan object that results from the calculation will contain the remaining time. For instance:

' Get the total desired time for count down
Dim total As New TimeSpan(Integer.Parse(Hours.Text), Integer.Parse(Minutes.Text), Integer.Parse(Seconds.Text))

' Later, get the time left and display on screen
Dim timeLeft As TimeSpan = total - stopwatch.Elapsed
Hours.Text = timeLeft.TotalHours
Minutes.Text = timeLeft.TotalMinutes
Seconds.Text = timeLeft.TotalSeconds

Obviously, just calling Integer.Parse like that isn't safe because the text boxes may contain non-numeric values. You will need to add code to handle that. For instance, if you want to just default to zero if an invalid entry is made, you could do something like this:

Dim totalHours As Integer
Dim totalMinutes As Integer
Dim TotalSeconds As Integer
Integer.TryParse(Hours.Text, totalHours)
Integer.TryParse(Minutes.Text, totalMinutes)
Integer.TryParse(Seconds.Text, totalSeconds)
Dim total As New TimeSpan(totalHours, totalMinutes, totalSeconds)

Or, if you want to display a validation error message, you could do something like this:

Dim total As TimeSpan
Try
    total = New TimeSpan(Integer.Parse(Hours.Text), Integer.Parse(Minutes.Text), Integer.Parse(Seconds.Text))
Catch ex As FormatException
    MessageBox.Show("Entries must be numeric.")
End Try
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • Thanks a Lot! Actually i am creating the Countdown for the very 1st time :) So just messed up with logics. Thanks for clearing my doubts – Saurabh Sharma Sep 20 '12 at 17:52
  • I am Getting a runtime error here: 'Dim total As New TimeSpan(Integer.Parse(Hours.Text), Integer.Parse(Minutes.Text), Integer.Parse(Seconds.Text))' Input string was not in a correct format. – Saurabh Sharma Sep 20 '12 at 17:59
  • You will have add code to handle invalid inputs. You could do so either by putting a try/catch block around that line or by using `Integer.TryParse` instead of `Integer.Parse`. – Steven Doggart Sep 20 '12 at 18:01
  • Ahan not working Error: Overload resolution failed because no accessible 'TryParse' accepts this number of arguments. – Saurabh Sharma Sep 20 '12 at 18:08
  • Still not working :/ `Dim total As New TimeSpan(Integer.Parse(Hours.Text), Integer.Parse(Minutes.Text), Integer.Parse(Seconds.Text))` Error: Input string was not in a correct format. – Saurabh Sharma Sep 20 '12 at 18:23
  • Are you sure you definitely have that try/catch block around it? Did you try running the application outside of the debugger? Depending on your debugger settings, it may break on even handled exception. – Steven Doggart Sep 20 '12 at 18:26
  • Yeap i just added `Dim totalHours As Integer Dim totalMinutes As Integer Dim TotalSeconds As Integer Integer.TryParse(Hours.Text, totalHours) Integer.TryParse(Minutes.Text, totalMinutes) Integer.TryParse(Seconds.Text, totalSeconds) Dim total As New TimeSpan(totalHours, totalMinutes, totalSeconds)` before calling `start()' – Saurabh Sharma Sep 20 '12 at 18:28