Timer pause/resume

jonathen

Member
Joined
May 12, 2005
Messages
17
Programming Experience
1-3
Hi guys,

Sadly, a newbie question for you! I've just started playing with the timer in vb.net and am trying to use a start/pause/resume button to start/pause/resume a progress bar I am using. I can make it start and stop the progress bar (so it appears to pause halfway through for example) but when I click start again, it starts from the beginning, obviously.

It seems the timer control does not have a pause() or resume(). I wish to be able to get the progress bar to continue from where it paused. Additionally, I want it to loop - should the progress bar get to the end before being paused, it shoud begin again.

Any help would be appreciated as I'm stuck here!

Thanks.

Jon
 
It sounds to me like you may need a boolean variable to check when you hit your button..

Dim HasStarted As Boolean

Then when you hit pause to can disable your timer and set the has started variable to true. You will then need to check the value of this variable when you restart your timer. If it is true you can add code restart the progess bar without resetting it's value.
 
That should do it :

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Start [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]False

[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Timer_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load[INDENT]Button1.Text = "Start"
Timer1.Interval = 100
Timer1.Enabled = 
[/INDENT][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2]ProgressBar1.Maximum = 1000
ProgressBar1.Minimum = 0
[/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub

[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click[/SIZE][INDENT][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Start = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Button was labelled "Start" or "Resume" => Start or resume
[/COLOR][/SIZE][SIZE=2]Button1.Text = "Pause"
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Button was labelled "Pause" => Pause timer
[/COLOR][/SIZE][SIZE=2]Button1.Text = "Resume"
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]Start = [/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] (Start) [/SIZE][SIZE=2][COLOR=#008000]'Invert boolean variable
[/COLOR][/SIZE][SIZE=2]Timer1.Enabled = Start
[/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub

[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Timer1_Tick([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Timer1.Tick[/SIZE][INDENT][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] ProgressBar1[INDENT].Increment(10)

[/INDENT][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] .Value = .Maximum [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2].Value = 0
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
Back
Top