计时器倒计时如14,13,12,11,10,9我想知道如何在倒计时时使9到09,所以它会像11,10,10,08,07 - 00 ,我已经尝试过goggling,但我不准确搜索关键字
我的计时器代码
Timer1.Interval = 1000
If hours_label.Text = "" Then
hours_label.Text = "0"
End If
If minutes_label.Text = "" Then
minutes_label.Text = "0"
End If
If seconds_label.Text = "" Then
seconds_label.Text = "0"
End If
If hours_label.Text = "00" Then
hours_label.Text = "0"
End If
If minutes_label.Text = "00" Then
minutes_label.Text = "0"
End If
If seconds_label.Text = "00" Then
seconds_label.Text = "0"
End If
If seconds_label.Text > "0" Then
seconds_label.Text = seconds_label.Text - 1
End If
If minutes_label.Text > "0" Then
If seconds_label.Text = "0" Then
minutes_label.Text = minutes_label.Text - 1
seconds_label.Text = "59"
End If
End If
If hours_label.Text > "0" Then
If minutes_label.Text = "0" Then
If seconds_label.Text = "0" Then
hours_label.Text = hours_label.Text - 1
minutes_label.Text = "59"
seconds_label.Text = "59"
End If
End If
End If
If hours_label.Text = "0" Then
If minutes_label.Text = "0" Then
If seconds_label.Text = "0" Then
Timer1.Enabled = False
msgbox("Times Up")
Screen_Locker.Show()
Me.Close()
End If
End If
End If
提前感谢您的回答
答案 0 :(得分:8)
这应该让你先行一步:使用PadLeft函数
Dim i As Integer = 15
While i >= 0
Console.WriteLine(i.ToString().PadLeft(2, "0"c))
i = i - 1
End While
工作原理:如果值是2位,例如14,它保持不变,但如果它是1位数,则在它之前附加“0”使其成为2位数(带2个字符的字符串)
12 = 12
11 = 11
10 = 10
9 = 09
...
0 = 00
答案 1 :(得分:1)
您可以使用
Dim s As String
s=Format(Integer,"0#")
只需将Integer替换为您要格式化的值或变量即可。这意味着如果那里没有任何东西,则第一个字符将为0(整数小于10),第二个字符为数字。
答案 2 :(得分:0)
或者使用来自@srka的字符串格式,只需少量出价代码:
Dim number As Int32 = 6
number.ToString("0#")