新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
'添加一个label标签名字label1 用来显示时间
创新互联专注于嘉祥网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供嘉祥营销型网站建设,嘉祥网站制作、嘉祥网页设计、嘉祥网站官网定制、小程序定制开发服务,打造嘉祥网络公司原创品牌,更为您提供嘉祥网站排名全网营销落地服务。
'再添加一个timer控件 名字timer1 interval属性=1000 用来计时
'窗体添加代码
Dim t As Date '用来记录时间
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Timer1.Tick
t = t.AddSeconds(1)
Label1.Text = "登录时间:" t.TimeOfDay.ToString
End Sub
Dim tCount As Integer '用来计数
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
tCount = 10
Timer1.Interval = 1000 '每秒执行一次
Timer1.Enabled = True
End
Sub
Private Sub
Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
tCount -= 1
If tCount = 0 Then
MessageBox.Show("时间到")
Timer1.Enabled = False
End If
End
Sub
不对。步骤如下:
添加一个label标签名字label1 用来显示时间
再添加一个timer控件 名字timer1 interval属性=1000 用来计时
窗体添加代码
Dim t As Date '用来记录时间
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Timer1.Tick
t = t.AddSeconds(1)
Label1.Text = "登录时间:" t.TimeOfDay.ToString
End Sub
控制台调用Timer和窗体是类似的。首先在项目引用里面加入System.Windows.Forms程序集,然后在代码顶部引入命名空间:
Imports System.Windows.Forms
在控制台的Module中声明一个计时器:
Private WithEvents Timer1 As New Timer()
把计时器的Tick事件静态绑定到处理函数中:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'一些代码
End Sub
在需要开始计时的地方,修改其Interval、Enabled属性:
Timer1.Interval = 1000
Timer1.Enabled = True
不需要控件就可以,用多线程的方式实现,在窗体上放一个Label和一个button按钮,代码如下:
Imports System.Threading
Public Class Form1
Dim Start As Double
Dim td As Thread
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Text = "00:00:00 000"
Button1.Text = "开始"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "开始" Then
Button1.Text = "结束"
Start = DateAndTime.Timer
td = New Thread(AddressOf fun1)
td.Start()
Else
Button1.Text = "开始"
td.Abort()
End If
End Sub
'定义一个线程
Private Sub fun1()
Dim elapsed As TimeSpan
Dim Dg_txt As New Dg(AddressOf txt)
Do
elapsed = TimeSpan.FromMilliseconds((DateAndTime.Timer - Start) * 1000)
Me.Invoke(Dg_txt, elapsed.ToString, Label1)
Thread.Sleep(100)
Loop
End Sub
'定义一个委托
Delegate Sub Dg(ByVal s As String, ByVal obj As Label)
Sub txt(ByVal s As String, ByVal obj As Label)
obj.Text = s
End Sub
End Class
Dim hour, min, sec As Integer
Private Sub Command1_Click()
If Command1.Caption = "开始计时" Then
Command1.Caption = "停止计时"
Timer1.Enabled = True
Else
If Command1.Caption = "停止计时" Then
Command1.Caption = "开始计时"
Timer1.Enabled = False
End If
End If
End Sub
Private Sub Form_Load()
hour = 0
min = 0
sec = 0
Label1.Caption = Format(hour, "00") ":" Format(min, "00") ":" Format(sec, "00")
End Sub
Private Sub Timer1_Timer()
sec = sec + 1
If sec 59 Then
sec = 0
min = min + 1
If min 59 Then
min = 0
hour = hour + 1
End If
End If
Label1.Caption = ""
Label1.Caption = Format(hour, "00") ":" Format(min, "00") ":" Format(sec, "00")
End Sub