计时器
这是我做的一个倒计时器,望大家多多指教!
一、新建 窗口应用程序,并命名。
二、选择“项目”--》“显示所有文件”
三、在“解决方案资源管理器”中找到“Form1.vb”,单击右键,“查看代码”
输入以下内容:
Public Class Form1
Dim InputTime As Integer '起始时间
Dim ShowComon As Boolean '是否显示冒号
REM 定义 时、他、秒
Dim H, M, S As String
REM 定义过程,计算剩余时间
Public Sub LeftTime()
Dim pH, pM, pS As Integer
pH = InputTime \ 3600 '计算输入小时数
InputTime = InputTime Mod 3600
pM = InputTime \ 60 '计算分钟数
pS = InputTime Mod 60 '剩余为秒数
H = Format(pH, "00")
M = Format(pM, "00")
S = Format(pS, "00")
End Sub
REM 计时器时间
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
REM 计时器 Tick 事件是处理方法,即每次计时即运行该过程
If InputTime <= 0 Then
REM 如果计时器已完成,则提示时间到
BeginEnd.Enabled = False
Timer1.Enabled = False
MessageBox.Show("时间到", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, 0, False)
REM 停止计时器的计时
Exit Sub
End If
If ShowComon Then
REM 如果上一次显示过冒号,则本次不显示了
REM 计时减 1
InputTime -= 1
REM 计算剩余时间
LeftTime()
REM 显示剩余时间
ShowTime.Text = H + " " + M + " " + S
REM 设置本次未显示冒号
ShowComon = False
Else
REM 如果上一次没有显示冒号,本次显示冒号
ShowTime.Text = H + ":" + M + ":" + S
REM 设置冒号为已显示
ShowComon = True
End If
End Sub
REM 初始化程序
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
H = "00"
M = "00"
S = "00"
ShowTime.Text = "00:00:00"
Timer1.Interval = 500 '实现秒计时
End Sub
REM 设置时间
Private Sub SetTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetTime.Click
Dim InputString As String
InputString = Trim(InputBox("请输入倒计时的秒数", "倒计时"))
If InputString = "" Then
Exit Sub
End If
InputTime = CInt(InputString)
LeftTime()
ShowTime.Text = H + ":" + M + ":" + S
BeginEnd.Enabled = True
If Timer1.Enabled Then
BeginEnd.Text = "停止倒计时"
Else
BeginEnd.Text = "开始倒计时"
End If
End Sub
REM 停止倒计时
Private Sub BeginEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BeginEnd.Click
If Timer1.Enabled Then
Timer1.Stop()
BeginEnd.Text = "开始倒计时"
Else
Timer1.Start()
BeginEnd.Text = "停止倒计时"
End If
End Sub
End Class
五、为了大家便于设计,所以可以直接用同样的方法在Form1.vb下面的“Form1.Designer.vb”中输入以下代码:
!!!!注意,以下代码不能改动,除非你很了解它的作用!!!!
<Global.Microsoft.()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form 重写 Dispose,以清理组件列表。
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Windows 窗体设计器所必需的
Private components As
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改它。
'不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
= New
Me.Timer1 = New System.Windows.Forms.Timer()
Me.ShowTime = New System.Windows.Forms.Label
Me.SetTime = New System.Windows.Forms.Button
Me.BeginEnd = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Timer1
'
Me.Timer1.Interval = 500
'
'ShowTime
'
Me.ShowTime.BackColor = System.Drawing.Color.Black
Me.ShowTime.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.ShowTime.Font = New System.Drawing.Font("Arial", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.ShowTime.ForeColor = System.Drawing.Color.Red
Me.ShowTime.Location = New System.Drawing.Point(34, 9)
Me.ShowTime.Name = "ShowTime"
Me.ShowTime.Size = New System.Drawing.Size(143, 36)
Me.ShowTime.TabIndex = 0
Me.ShowTime.Text = "00:00:00"
Me.ShowTime.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'SetTime
'
Me.SetTime.Location = New System.Drawing.Point(12, 57)
Me.SetTime.Name = "SetTime"
Me.SetTime.Size = New System.Drawing.Size(89, 37)
Me.SetTime.TabIndex = 1
Me.SetTime.Text = "设置起始时间"
Me.SetTime.UseVisualStyleBackColor = True
'
'BeginEnd
'
Me.BeginEnd.Location = New System.Drawing.Point(107, 57)
Me.BeginEnd.Name = "BeginEnd"
Me.BeginEnd.Size = New System.Drawing.Size(89, 37)
Me.BeginEnd.TabIndex = 2
Me.BeginEnd.Text = "开始倒计时"
Me.BeginEnd.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(208, 108)
Me.Controls.Add(Me.BeginEnd)
Me.Controls.Add(Me.SetTime)
Me.Controls.Add(Me.ShowTime)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Friend WithEvents Timer1 As System.Windows.Forms.Timer
Friend WithEvents ShowTime As System.Windows.Forms.Label
Friend WithEvents SetTime As System.Windows.Forms.Button
Friend WithEvents BeginEnd As System.Windows.Forms.Button
End Class