VB 製作乙個簡單的計時器

2021-08-30 23:36:34 字數 1830 閱讀 5459

1.新增乙個label 用於顯示時間

2.新增乙個timer空間並將interval屬性設為1000(毫秒)。enabled屬性設定為false

3.新增3個command 分別為「設定時間」,「開始」,「結束」

4.新增乙個模組,並在模組中新增以下**用於時間轉換:

option explicit

public hh, mm, ss as integer

public function hmsvaluetostring(byval h as integer, byval m as integer, byval s as   integer) as string

dim hstring, mstring, sstring as string

if h < 10 then

hstring = "0" + trim(str(h))

else

hstring = trim(str(h))

end if

if m < 10 then

mstring = "0" + trim(str(m))

else

mstring = trim(str(m))

end if

if s < 10 then

sstring = "0" + trim(str(s))

else

sstring = trim(str(s))

end if

hmsvaluetostring = hstring + ":" + mstring + ":" + sstring

end function

5.新增設定時間**

private sub command1_click()

dim temp, hs, ms as string, valuetime as integer

temp = inputbox("請輸入倒計時數(以分鐘為單位):", "設定倒計時")

valuetime = val(temp)

hh = int(valuetime / 60)  '獲得小時部分

mm = valuetime - hh * 60

ss = 0

label1.caption = hmsvaluetostring(hh, mm, ss)

end sub

6.倒計時開始**,將timer空間的enabled屬性設定為true.開始計時呼叫timer1_timer()

方法private sub command2_click()

timer1.enabled = true   

end sub

private sub timer1_timer()

if ss < 1 then '如果秒位已為0,則向高位借

if mm < 1 then  '若分位也已為0,則向小時位藉

if hh < 1 then

timer1.enabled = false

exit sub

else

hh = hh - 1

mm = 59

ss = 60

end if

else

mm = mm - 1

ss = 60

end if

end if

ss = ss - 1    '秒數減1

'------將本次剩餘時間轉換為hh:mm:ss格式

label1.caption = hmsvaluetostring(hh, mm, ss)

end sub

7.結束計時

private sub command3_click()

endend sub

乙個簡單的計時器

果然是新手啊,現在的階段是什麼都要上網查,只知道實現原理,demo卻寫不出來。哎 第乙個練手的,乙個簡單的計時器,基本原理時使用執行緒控制textview的定時重新整理 view code 1 package king.timer 23 import 4import android.os.bundl...

乙個簡單地計時器。

這是乙個很簡單地秒錶計時器,以一秒為單位計時,實現了暫停時間和繼續計時兩個功能。介面如下 如下 計時開始按鈕 void start id sender 計時停止按鈕 void stop id sender 初始化 label uilabel alloc initwithframe cgrectmak...

Python學習 乙個簡單的計時器

在實際開發中,往往想要計算一段 執行多長時間,下面我將該功能寫入到乙個函式裡面,只要在每個函式前面呼叫該函式即可,見下面 乙個記時器,只要在函式前面寫上 fun timer即可 import time from functools import wraps deffun timer function...