How to display the remaining time in a countdown timer?

In VB, you can use the Timer control to implement a countdown and display the remaining time. Here is an example code:

  1. Add a Label control to the form and set its name to lblTime.
  2. Include two buttons, one for starting the countdown and one for stopping the countdown, and name them btnStart and btnStop respectively.
  3. Add the following code to the window’s form code:
Dim remainingTime As Integer = 60 ' 设置倒计时的初始时间
Dim timer As New Timer ' 创建一个新的Timer对象

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    timer.Interval = 1000 ' 设置计时器的间隔为1秒
    AddHandler timer.Tick, AddressOf Timer_Tick ' 指定计时器的Tick事件处理程序
End Sub

Private Sub Timer_Tick(sender As Object, e As EventArgs)
    If remainingTime > 0 Then
        remainingTime -= 1 ' 每秒减少1
        lblTime.Text = remainingTime.ToString() ' 更新Label控件的文本
    Else
        timer.Stop() ' 如果倒计时为0,则停止计时器
        MessageBox.Show("倒计时结束!")
    End If
End Sub

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    timer.Start() ' 开始计时
End Sub

Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
    timer.Stop() ' 停止计时
End Sub

The code above will create a 60-second countdown timer and display the remaining time in a Label control. When the countdown reaches zero, a message box will pop up notifying the end of the countdown. You can customize the initial countdown time and the message displayed in the message box as needed.

bannerAds