VB 2012 Progress Bar to Track remaining time
VB 2012 Progress Bar to Track remaining time
VB 2012 Progress Bar to Track remaining time
VB 2012 Progress Bar to Track remaining time
VB 2012 Progress Bar to Track remaining time
VB 2012 Progress Bar to Track remaining time VB 2012 Progress Bar to Track remaining time VB 2012 Progress Bar to Track remaining time VB 2012 Progress Bar to Track remaining time VB 2012 Progress Bar to Track remaining time VB 2012 Progress Bar to Track remaining time VB 2012 Progress Bar to Track remaining time VB 2012 Progress Bar to Track remaining time
VB 2012 Progress Bar to Track remaining time VB 2012 Progress Bar to Track remaining time
VB 2012 Progress Bar to Track remaining time
Go Back  Xtreme Visual Basic Talk > > > VB 2012 Progress Bar to Track remaining time


Reply
 
Thread Tools Display Modes
  #1  
Old 07-26-2014, 02:48 PM
xcpcx24 xcpcx24 is offline
Newcomer
 
Join Date: Jul 2014
Posts: 3
Default VB 2012 Progress Bar to Track remaining time


I am really new to programming ( i like it a lot) I am creating a program that basically compares two times. (similar to an alarm) once the time is up it will pop out a message indicating that time is up!!! In addition I would like to integrate a progressbar that would load based on the the remaining time before the time is up. I am stock at the progress bar I am not sure what to do or where to go from here. Can any one provide me with some suggestions/guidance or a code i can take a look at for reference?

My code:
Code:
 Imports System.Net.Mime.MediaTypeNames

Public Class Form1
    Private hour As Integer = 0
    Private minute As Integer = 0
    Private second As Integer = 0

    Public Sub show_time()
        second += 1
        If second = 59 Then
            second = 0
            minute += 1
            If minute = 59 Then
                minute += 1
                hour += 1
            End If
        End If

        Label3PrgressStdPC.Text = hour.ToString.PadLeft(2, "0") & ":"
        Label3PrgressStdPC.Text &= minute.ToString.PadLeft(2, "0") & ":"
        Label3PrgressStdPC.Text &= second.ToString.PadLeft(2, "0")
        Label3PrgressStdPC.Refresh()

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        show_time()
        If TextBox1.SelectedText = TextBox1.Text Then Exit Sub
        If TextBox1.Text = Label3PrgressStdPC.Text Then
            Timer1.Stop()
            MsgBox("time is up")
            
        End If
    End Sub

    Private Sub Bn_start_St01_Click(sender As Object, e As EventArgs) Handles Bn_start_St01.Click
        Timer1.Start()
        Timer1.Enabled = True
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = Val(TextBox1.Text) * 60
        ProgressBar1.Value = 0
        Timer2.Start()
        Timer2.Enabled = True

    End Sub

   

    Private Sub ProgressBar1_Click(sender As Object, e As EventArgs) Handles ProgressBar1.Click
        ProgressBar1.Maximum = Val(TextBox1.SelectedText)
        ProgressBar1.Minimum = 0

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ProgressBar1.Maximum = Val(TextBox1.Text)
        ProgressBar1.Maximum = 0
    End Sub

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        ProgressBar1.Value = ProgressBar1.Value +1'  I dont know what to do from here i need help
 



    End Sub
End Class
_________________________________________________________________


Thank you

Last edited by passel; 07-29-2014 at 07:47 AM. Reason: Added [code][/code] tags
Reply With Quote
  #2  
Old 07-29-2014, 09:08 AM
passel's Avatar
passelVB 2012 Progress Bar to Track remaining time passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

Counting time using timer ticks is not very accurate, the tick intervals are not consistent and your time count versus actual time will drift.
I also noticed that whatever time was enter, say 2 minutes, ran 2:02 or 5 minutes, ran 5:05.
That is because you made each minute 59 seconds, instead of 60 (same with minutes to hours, but I never ran that long).

You can use the timer for periodically updating the GUI, but you need a more accurate source for counting the passage of time. You can use the time of day, and various other means, but I usually prefer using a StopWatch object.
I usually use the ElapsedMilliseconds property for seeing how much time has passed.
Also, it is best to use a single timer and some logic within the timer to do different things at different rates, if needed, rather than use multiple timers.

So, I just played around with the code a bit and made some modifications to use a stopwatch.
It seems like what you had for the ProgressBar was pretty much OK, tt just didn't need a separate timer and perhaps a little protection to make sure you don't exceed the maximum value.

You were multiplying Textbox1.Text by 60, so my assumption is the user enters how many minutes they want to time and you're converting that to seconds. I further convert that to milliseconds where needed to compare to the stopwatch and stop when the allotted time has passed.
You could subtract the elapsed time from the maximum value instead, so the bar starts at maximum and shrinks down to 0 when the time runs out.
Code:
    ProgressBar1.Value = Math.Max(ProgressBar1.Maximum - csw, 0) 'reduced down to 0 over time
I did change the time display to take advantage of some of the built in formatting, using TimeSpan to convert elapsed milliseconds to seconds, and then timeSpan to DateTime for display as hh:mm:ss.

I have Option Strict turned on by default, so had to add a CInt a couple of places to satisfy implicit conversions. You should probably have Option Strict turned on as well, so you get used to explicitly matching data types or converting when necessary.
Code:
'Imports System.Net.Mime.MediaTypeNames

Public Class Form1
  'Private hour As Integer = 0
  'Private minute As Integer = 0
  'Private second As Integer = 0
  Private EndTime As Integer

  Private sw As New Stopwatch

  Public Sub show_time()
    'second += 1
    'If second = 60 Then
    '  second = 0
    '  minute += 1
    '  If minute = 60 Then
    '    minute += 1
    '    hour += 1
    '  End If
    'End If

    'Label3PrgressStdPC.Text = hour.ToString.PadLeft(2, "0") & ":"
    'Label3PrgressStdPC.Text &= minute.ToString.PadLeft(2, "0") & ":"
    'Label3PrgressStdPC.Text &= second.ToString.PadLeft(2, "0")
    '  Label3PrgressStdPC.Refresh()
    Dim ti As TimeSpan = TimeSpan.FromSeconds(sw.ElapsedMilliseconds / 1000)
    Dim td As DateTime = New DateTime(ti.Ticks)
    Label3PrgressStdPC.Text = td.ToString("HH:mm:ss")

  End Sub

  Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim csw As Integer = CInt(sw.ElapsedMilliseconds) 'assume won't run sw long enough (around 25 days) to exceed an Integer

    '    ProgressBar1.Value = ProgressBar1.Value + 1 '  I dont know what to do from here i need help
    ProgressBar1.Value = Math.Min(csw, ProgressBar1.Maximum) 'don't exceed ProgressBar1.Maximum value

    show_time()
    If TextBox1.SelectedText = TextBox1.Text Then Exit Sub
    '  If TextBox1.Text = Label3PrgressStdPC.Text Then
    ' If ElapsedTime = EndTime Then
    If csw >= EndTime * 1000 Then
      Timer1.Stop()
      MsgBox("time is up")

    End If
  End Sub

  Private Sub Bn_start_St01_Click(sender As Object, e As EventArgs) Handles Bn_start_St01.Click
    sw.Reset()
    EndTime = CInt(Val(TextBox1.Text) * 60)
    ProgressBar1.Minimum = 0
    ProgressBar1.Maximum = EndTime * 1000
    ProgressBar1.Value = 0
    sw.Start()
    Timer1.Start()
    'Timer2.Start()
    'Timer2.Enabled = True

  End Sub

  'Private Sub ProgressBar1_Click(sender As Object, e As EventArgs) Handles ProgressBar1.Click
  '  ProgressBar1.Maximum = Val(TextBox1.SelectedText)
  '  ProgressBar1.Minimum = 0

  'End Sub

  'Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  '  ProgressBar1.Maximum = Val(TextBox1.Text)
  '  ProgressBar1.Maximum = 0
  'End Sub

End Class
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 07-29-2014 at 09:16 AM.
Reply With Quote
  #3  
Old 08-03-2014, 06:59 PM
xcpcx24 xcpcx24 is offline
Newcomer
 
Join Date: Jul 2014
Posts: 3
Default

Thanks for the help so much!!!!!.....I need help adding a progressbar that would load based on the the remaining time before the time alarm goes off.
Code:
Public Class Form1

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        Label1.Text = Format(TimeOfDay, "HH:mm:ss")
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If TimeOfDay = Label2.Text Then
            MsgBox("time is up")
            ProgressBar1.Minimum = 0
            ProgressBar1.Maximum = Val(MaskedTextBox1.Text) * 1000
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Label2.Text = MaskedTextBox1.Text
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Timer1.Start()

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        End
    End Sub
End Class
Thank you

Last edited by passel; 08-05-2014 at 02:50 PM. Reason: Put code between [code][/code] tags.
Reply With Quote
  #4  
Old 08-05-2014, 03:29 PM
passel's Avatar
passelVB 2012 Progress Bar to Track remaining time passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

This is one way you could go about it.

I don't have your MaskedTextBox1 setup, so I just changed it to read the value from TextBox1.Text, as I typed in in, e.g. 17:21
Parsed that value into a Date time, and used DateDiff to get the number of seconds from "Now" till then, setting ProgressBar max to that.
Originally had the ProgressBar show remaining time, so it started near the max value and decremented down. Commented out those two lines and had it go the other way, starting near 0 and incrementing to Max value.
You can choose which way you want to go.
Now in the timer, it simply calculates that remaining time the same way, and updates the progress bar.
If the remaining time reaches 0 (or less), then the time is up.
Code:
  Dim AlarmTime As Date
  Dim RemainingTime As Long

  Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
    Label1.Text = Format(TimeOfDay, "HH:mm:ss")
  End Sub

  Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    RemainingTime = DateDiff(DateInterval.Second, Now, AlarmTime)
    If RemainingTime <= 0 Then
      Timer1.Stop()
      '     ProgressBar1.Value = 0
      ProgressBar1.Value = ProgressBar1.Maximum
      ProgressBar1.Refresh()
      MsgBox("time is up")
    Else
      '      ProgressBar1.Value = CInt(RemainingTime)
      ProgressBar1.Value = ProgressBar1.Maximum - CInt(RemainingTime)
    End If
  End Sub

  'Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  '  Label2.Text = MaskedTextBox1.Text
  'End Sub

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Label2.Text = TextBox1.Text
    AlarmTime = Date.Parse(Label2.Text)
    RemainingTime = DateDiff(DateInterval.Second, Now, AlarmTime)
    ProgressBar1.Maximum = CInt(RemainingTime)

    Timer1.Start()
  End Sub

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Timer2.Start()
  End Sub
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #5  
Old 08-06-2014, 04:26 PM
xcpcx24 xcpcx24 is offline
Newcomer
 
Join Date: Jul 2014
Posts: 3
Default

Thank you passel you saved me. I am curious as to how could I add countdown clock into the program, I am thinking into adding one more label to turned it into the countdown clock, I basically need the countdown clock to count the sec and min,before the alrrm goes off. I am planning on adding something like, do you think this will it work?

Dim AlarmTime As Date
Dim RemainingTime As Long

Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
Label1.Text = Format(TimeOfDay, "HH:mm:ss")
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
RemainingTime = DateDiff(DateInterval.Second, Now, AlarmTime)
If RemainingTime <= 0 Then
Timer1.Stop()
' ProgressBar1.Value = 0
ProgressBar1.Value = ProgressBar1.Maximum
ProgressBar1.Refresh()
MsgBox("time is up")
Else
' ProgressBar1.Value = CInt(RemainingTime)
ProgressBar1.Value = ProgressBar1.Maximum - CInt(RemainingTime)
End If
End Sub

'Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' Label2.Text = MaskedTextBox1.Text
'End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label2.Text = TextBox1.Text
AlarmTime = Date.Parse(Label2.Text)
RemainingTime = DateDiff(DateInterval.Second, Now, AlarmTime)
ProgressBar1.Maximum = CInt(RemainingTime)

label4.Text= format(RemainingTime - CInt(RemainingTime),"HH:mm:ss")

Timer1.Start()

End Sub

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer2.Start()
End Sub
Reply With Quote
  #6  
Old 08-06-2014, 08:09 PM
passel's Avatar
passelVB 2012 Progress Bar to Track remaining time passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

RemainingTime is the remaining time in seconds.
It is a Long Type, which is a 64-bit Integer.
The CInt(RemainingTime) is used a few places because Option Strict On won't allow assigning a bigger Integer Type (i.e. a Long 64-bit) to a smaller Integer Type (Integer is 32-bit). You have to use CInt to convert the 64-bit integer into a 32-bit integer.

So, if RemainingTime is something like 5 minutes (300 seconds),
RemainingTime - CInt(RemainingTime) is (300 - 300) so is 0.
It will always be 0.

You just need to convert the Seconds into a TimeSpan, and then the TimeSpan into a Date, and then the Date into "mm:ss", which could be like:
Code:
Label3.Text = New DateTime(TimeSpan.FromSeconds(RemainingTime).Ticks).ToString("mm:ss")
Two lines added to the Timer1 tick event in the previous example (plus the added label3)
Code:
  Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    RemainingTime = DateDiff(DateInterval.Second, Now, AlarmTime)
    If RemainingTime <= 0 Then
      Label3.Text = "00:00"               'new code, set countdown display to 00:00
      Timer1.Stop()
      '     ProgressBar1.Value = 0
      ProgressBar1.Value = ProgressBar1.Maximum
      ProgressBar1.Refresh()
      MsgBox("time is up")
    Else
      'new code, set countdown display
      Label3.Text = New DateTime(TimeSpan.FromSeconds(RemainingTime).Ticks).ToString("mm:ss")
      '      ProgressBar1.Value = CInt(RemainingTime)
      ProgressBar1.Value = ProgressBar1.Maximum - CInt(RemainingTime)
    End If
  End Sub
p.s. I guess I didn't really notice it the first time, but looking at the code again (especially since you use the Format function), I guess in .Net it would be preferable to not use the Format function as you did in the Timer2's Tick event, but rather used the object's .ToString method.
Code:
  Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
    Label1.Text = TimeOfDay.ToString("HH:mm:ss") 'Format(TimeOfDay, "HH:mm:ss")
  End Sub
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 08-06-2014 at 08:28 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
VB 2012 Progress Bar to Track remaining time
VB 2012 Progress Bar to Track remaining time
VB 2012 Progress Bar to Track remaining time VB 2012 Progress Bar to Track remaining time
VB 2012 Progress Bar to Track remaining time
VB 2012 Progress Bar to Track remaining time
VB 2012 Progress Bar to Track remaining time VB 2012 Progress Bar to Track remaining time VB 2012 Progress Bar to Track remaining time VB 2012 Progress Bar to Track remaining time VB 2012 Progress Bar to Track remaining time VB 2012 Progress Bar to Track remaining time VB 2012 Progress Bar to Track remaining time
VB 2012 Progress Bar to Track remaining time
VB 2012 Progress Bar to Track remaining time
 
VB 2012 Progress Bar to Track remaining time
VB 2012 Progress Bar to Track remaining time
 
-->