Dopey-Rob

I'm currently working on a project, and I wanted to include a timer.

I want the timer to count down to zero, and when the timer gets to zero something could happen..(for example.. a message box could appear)

How can I do this

Could someone give me code Big Smile (I'm lazy)

Thanks,

Rob




Re: Visual Basic Express Edition Timers.. HOW?

ahmedilyas

Timers don't do countdowns but increase by the specified timer interval.

Drag a timer on the form and set its Timer Interval property to "1000" for 1 second

Drag a button on the form

Double click the timer to make the timer ticked event

Double click the button to make a button click event

in the button click event, start the timer:

Me.timer1.Start()

in the timer ticked event, (this will fire everytime the timer interval has past), place your code here. An example:

MessageBox.Show(DateTime.Now.ToString())

to do a countdown, one method is to declare a global variable and set its variable to 10. Then on every timer tick event, decrement it. Example (if your variable name was called countdown)

Me.countdown = Me.countdown - 1

if Me.countdown = 0 then

MessageBox.Show("countdown is 0")

Me.timer1.Stop() 'stops the timer now, it will not execute any more until you call the Start() method again

end if

this will decrement the countdown variable by 1, then show a MessageBox when the condition evaluates to when the variable is 0 and finally stops the timer.

Does this help






Re: Visual Basic Express Edition Timers.. HOW?

VBX-Robs

thanksBig Smile






Re: Visual Basic Express Edition Timers.. HOW?

Solitaire

I posted this a few weeks ago. If you place the changing time in the form's title bar, you can see it in the taskbar even if you have another application on the desktop. This program uses a textbox, a label, and two buttons. You can dress up the code a bit and have a pause button in addition to the stop button.

Code Snippet

Public Class Form1

Private counter As Integer

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

Integer.TryParse(txtCount.Text, counter)

If counter <= 0 Then counter = 10 'must be >0 or endless

txtCount.Text = counter.ToString

tmrCount.Enabled = True

lblSeconds.Text = counter.ToString

End Sub

Private Sub tmrCount_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrCount.Tick

If counter = 1 Then

tmrCount.Enabled = False

lblSeconds.Text = "0"

Me.Text = "Count Down: 0"

MessageBox.Show("Time is up")

btnStop.PerformClick()

Else

counter = counter - 1

lblSeconds.Text = counter.ToString

End If

Me.Text = "Count Down: " & counter.ToString

End Sub

Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click

tmrCount.Enabled = False

lblSeconds.Text = ">"

Me.Text = "Count Down"

txtCount.Clear()

txtCount.Focus()

End Sub

End Class






Re: Visual Basic Express Edition Timers.. HOW?

VBX-Robs

i just need the code for a simple timer, press a button, timer = 10000, then a message after and stop the timer

and i want it so that you use the component timer