创建一个简单的计时器来计算秒、分和小时 [英] Create a simple timer to count seconds, minutes and hours

查看:42
本文介绍了创建一个简单的计时器来计算秒、分和小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个非常简单的程序,它基本上是一个计时器.我有三组标签,lbl_secondslbl_minuteslbl_hours.这些标签的默认值是 00:00,我希望计时器为每个标签更改该值.我用谷歌搜索过这个,但我似乎找不到任何关于它的好信息.

I'm trying to create a pretty simple program that basically is a timer. I have three sets of labels, lbl_seconds, lbl_minutes and lbl_hours. These labels have the default value of 00:00 and I want the timer to change that for each label. I have googled this but I cannot seem to find any good info on it.

我需要三个独立的计时器吗?我还注意到计时器有自己的滴答事件处理程序.我想正是在这一点上,我需要更改标签的值.如何做到这一点?

Do I need three separate timers? I have also noticed that the timers have their own tick event handler. I guess it's in this that I need to change the value of the label. How to do just that?

推荐答案

我认为你需要这样的东西

I think you need something of this sort

Public Function GetTime(Time as Integer) As String
    Dim Hrs        As Integer  'number of hours   '
    Dim Min        As Integer  'number of Minutes '
    Dim Sec        As Integer  'number of Sec     '

    'Seconds'
    Sec = Time Mod 60

    'Minutes'
    Min = ((Time - Sec) / 60) Mod 60

    'Hours'
    Hrs = ((Time - (Sec + (Min * 60))) / 3600) Mod 60

    Return Format(Hrs, "00") & ":" & Format(Min, "00") & ":" & Format(Sec, "00")
End Function

您传递想要在标签文本上显示的时间(以秒为单位),时间将按您喜欢的格式设置.

You pass the time (in seconds) you'd like to display on the label's text and the time will be formatted as you like it.

例如

lblTime.Text = GetTime(90)

这将在标签上显示 00:01:30.

作为参考,您可以查看这个项目 我前段时间在 FreeVBCode 上提交了.唯一需要注意的是该项目在 VB6 中.不过,您应该可以在 Visual Studio 中打开它.

For reference, you can see this project I submitted on FreeVBCode some time ago. The only caveat is the project is in VB6. You should be able to open it in Visual Studio though.

这篇关于创建一个简单的计时器来计算秒、分和小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆