Display progressbar showing progress of timer counting up to its Timer.interval

solorize

Member
Joined
Feb 22, 2012
Messages
7
Programming Experience
Beginner
Hi,

I am having a problem trying to do the following:

I have timer called" Timer1" that grabs some information to be displayed in a textbox which
has it's interval set by the user, by selecting one of a set of radio buttons, that offer the
user options of various time increments, i.e 15 sec, 30 sec etc.....

What I would like to do is have a ProgressBar shown on my form that counts up
to the Timer.Interval.

i.e The timer interval is set by the user to say and interval of 30seconds
then when a button is pressed, the progress bar starts to show the progress of the Timer1
counting up to 100% when it reaches the user selected Timer1.interval value of in this case 30seconds.

I have tried to do this last night but ended up in a right mess.

Could someone explain how I can go about doing this correctly?

As I am at work I do not have access to my code to show you what I have tried,
and therefore hope that my explanation above will be enough to show you what
I am trying to achieve.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
How are you going to change the ProgressBar? You'd need another Timer to do that. You'd need a Timer with an Interval of 1000 so that you could update the ProgressBar every second.

The more logical solution is to simply use one Timer with an Interval of 1000. If the user selects 30 seconds then you set the Maximum of the ProgressBar to 30. You call PerformStep on the ProgressBar each Tick and check the Value. If it is equal to the Maximum then you're done.
 

ProtekNickz

Well-known member
Joined
Oct 22, 2009
Messages
117
Location
UK
Programming Experience
1-3
you could use two timers or do this,

Code:
Option Strict On
Option Explicit On

'2 x Buttons
'2 x Labels
'1 x TextBox
'1 x ComboBox
'1 x ProgressBar

Public Class TimedProgress

    Private Sub TimedProgress_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        tmrTimer.Enabled = False 'Disables Timer on FormLoad
    End Sub

    'Once the SelectTime is initiated it then checks for the two Varibles 30 or 60 then sets the tmrTimer(Timer1) interval
    Public Sub SelectTime(ByVal tmrSeconds As Integer)
        pgbProgress.Minimum = 0 'set the ProgressBar Min Valu to 0
        If tmrSeconds = 30 Then tmrTimer.Interval = 30000 ' = 30 Seconds
        If tmrSeconds = 60 Then tmrTimer.Interval = 60000 ' = 60 Seconds
        pgbProgress.Maximum = tmrTimer.Interval 'Sets the Progress Bar Max Value to Desired Inteval
    End Sub
    'Selects the case in the cboSecond(ComboBox) to which value is selected then runs the correct Procedure
    Private Sub BtnRun_Click(sender As System.Object, e As System.EventArgs) Handles BtnRun.Click
        BtnRun.Enabled = False 'Disables The Run Button
        Select Case cboSeconds.Text
            Case "30 Sec" 'Compares text in cboSeconds
                SelectTime(30) 'Sets the Timer Interval
                tmrTimer.Enabled = True 'Enables The Timer
                pgbProgress.Value = tmrTimer.Interval 'Progresses to the timers interval an so on..
            Case "60 Sec"
                SelectTime(60)
                tmrTimer.Enabled = True
                pgbProgress.Value = tmrTimer.Interval
        End Select
    End Sub
    'Restet needed Controls and Timers
    Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
        BtnRun.Enabled = True
        tmrTimer.Enabled = False
        pgbProgress.Value = 0
    End Sub

End Class
just use as a guide, hope it helps. :)
i didnt do anything with the Textbox just added it :D
 

ProtekNickz

Well-known member
Joined
Oct 22, 2009
Messages
117
Location
UK
Programming Experience
1-3
Hi i open my mouth then realise that the above code it inaccurate, i didn't really test it too much, and at the speed of the progressbar, it's lightspeed for 30 and 60 seconds, ok i got that over and done with, here's the revised code, some of it mayt be long winded but that's what you get :D, THIS ONE WORKS, lol
Code:
Option Strict On
Option Explicit On

'2 x Buttons
'2 x Labels
'1 x TextBox
'1 x ComboBox
'1 x ProgressBar

Public Class TimedProgress
    Dim intVal As Integer = 0 'Declare value as integer for use in program
    Private Sub TimedProgress_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        tmrTimer.Enabled = False 'Disables Timer on FormLoad
    End Sub

    'Once the SelectTime is initiated it then checks for the two Varibles 30 or 60 then sets the tmrTimer(Timer1) interval
    Public Sub SelectTime(ByVal tmrSeconds As Integer, ByVal tmrInterVal As Integer)
        pgbProgress.Minimum = 0 'set the ProgressBar Min Valu to 0
        tmrTimer.Interval = tmrInterVal
        pgbProgress.Maximum = tmrSeconds 'Sets the Progress Bar Max Value to Desired Inteval
    End Sub
    'Selects the case in the cboSecond(ComboBox) to which value is selected then runs the correct Procedure
    Private Sub BtnRun_Click(sender As System.Object, e As System.EventArgs) Handles BtnRun.Click
        BtnRun.Enabled = False 'Disables The Run Button
        Select Case cboSeconds.Text
            Case "30 Sec" 'Compares text in cboSeconds
                SelectTime(30, 1000) 'Sets the Seconds to Count and Timer Interval
                tmrTimer.Enabled = True 'Enables The Timer
                cboSeconds.Enabled = False
            Case "60 Sec"
                SelectTime(60, 1000)
                tmrTimer.Enabled = True
                cboSeconds.Enabled = False
        End Select
    End Sub
    'Restet needed Controls and Timers
    Private Sub strReset()
        BtnRun.Enabled = True 'Resets Controls and Values to retry Program
        cboSeconds.Enabled = True
        tmrTimer.Enabled = False
        pgbProgress.Value = 0
        intVal = 0
    End Sub

    Private Sub tmrTimer_Tick(sender As System.Object, e As System.EventArgs) Handles tmrTimer.Tick
        intVal += 1 ' Add a value to intVal + intVal + 1 as the timer ticks over this Counts up from 1 to tmrSeconds
        pgbProgress.Value = intVal - 1 'Progresses to the timers interval an so on..
        If intVal = 31 And cboSeconds.Text = "30 Sec" Then
            strReset()
            MsgBox("Your 30 Seconds are up", MsgBoxStyle.OkOnly, "Time is up")
        ElseIf intVal = 60 And cboSeconds.Text = "60 Sec" Then
            strReset()
            MsgBox("Your 60 Seconds are up", MsgBoxStyle.OkOnly, "Time is up")
        End If
    End Sub
End Class
if you do manage to have problems with this one then just Message me.
 

brendon

New member
Joined
Feb 25, 2012
Messages
3
Location
Sydney, Australia
Programming Experience
5-10
solorize,

You are able to achieve this using only one timer, by relying to the value of the progress bar.

Code:
'    Assuming Radio Button #1 (15 Seconds) = rbSeconds15
'    Assuming Radio Button #2 (30 Seconds) = rbSeconds30
'    Assuming Button #1 (Start) = btnStart
'    Assuming Button #2 (Stop) = btnStop
'    Assuming Timer = timTimer
'    Assuming Progress Bar = pbProgress


Public Class ProgressTimer


    Private timerInt As Integer = 0


    Private Sub SetTimer(seconds As Integer)
        timerInt = CInt(seconds)
    End Sub


    Private Sub ProgressTimer_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        timTimer.Enabled = False
    End Sub


    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        '    Disable the start button, and enable the stop button.
        btnStart.Enabled = False
        btnStop.Enabled = True


        '    Get our selected radio button and determine our timer amount.
        If rbSeconds15.Checked = True Then
            SetTimer(15)
        ElseIf rbSeconds30.Checked = True Then
            SetTimer(30)
        Else
            MsgBox("Please select an amount of seconds.", DirectCast(MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, MsgBoxStyle), "Select a Timer Amount")
            Exit Sub
        End If


        '    Set the value of the progress bar.
        pbProgress.Value = 1
        pbProgress.Maximum = timerInt


        '    Start the timer.
        timTimer.Interval = 1000
        timTimer.Enabled = True
    End Sub


    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        '    Disable the stop button, and enable the start button.
        btnStop.Enabled = False
        btnStart.Enabled = True


        '    Stop the timer.
        timTimer.Enabled = False
    End Sub

     Private Sub timTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timTimer.Tick        '	Keep incrementing the progress bar every Tick until it reaches it's maximum.
        If pbProgress.Value >= pbProgress.Maximum Then
            '   Stop the timer.
            timTimer.Enabled = False


            '	Disable the stop button, and enable the start button.
            btnStop.Enabled = False
            btnStart.Enabled = True


            '	Trigger the TimerComplete procedure.
            pbProgress.Value = pbProgress.Maximum
            TimerComplete()
        Else
            pbProgress.Value += 1
        End If
    End Sub

    Private Sub TimerComplete()
        '    This is where you would grab the information, and display it in your TextBox 'txtInformation'.
        txtInformation.Text = "This is the 'TimerComplete()' procedure triggered after " + CStr(timerInt) + " seconds."
        MsgBox("This is the 'TimerComplete()' procedure triggered after " + CStr(timerInt) + " seconds.", DirectCast(MsgBoxStyle.Information + MsgBoxStyle.OkOnly, MsgBoxStyle), "Timer Complete")
    End Sub


End Class


Hope this helps.

Thanks,
Brendon
 
Last edited:

solorize

Member
Joined
Feb 22, 2012
Messages
7
Programming Experience
Beginner
Thanks for all your help, I now have it working.

Cheers

Mark
 
Top Bottom