我有一个简单的文本框和一个数字版本。 我需要将文本框中的结果转换为0:00:00时间格式。因此,如果numericupdown显示“2”,则文本框显示“1:58:00”。这是我正在研究的计时器,我只是不停地对着这个计时器。如果有人可以帮助这个简单的编码器阻止,我会非常感激!
Public Class Form1
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
TextBox1.Text = 120 - NumericUpDown1.Value
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = 120 - NumericUpDown1.Value
End Sub
End Class
答案 0 :(得分:3)
基本上有两种(好的)方法。
以下是(2)
的示例 dim baseTm as TimeSpan = New TimeSpan(2, 0,0)
Dim offset as TimeSpan = New TimeSpan(0,NumericUpDown1.Value, 0)
Console.WriteLine(baseTm.Subtract(offset).ToString("HH\:mm\:ss", New CultureInfo("en-US" )))
(1)的一个例子是:
Dim baseDt as DateTime =#2:00#
Console.WriteLine(baseDt.AddMinutes(-NumericUpDown1.Value).ToString("HH:mm:ss"))
你也可以自己做数学,但尽管很容易,但是为了覆盖所有的基础,使用日期更清楚了...
num = 120 - NumericUpDown1.Value
Dim hr as Integer = num \ 60
Dim mn As Integer = num - (hr*60)
Dim str As String = hr.ToString()
Dim str1 As String = mn.ToString("00")
TextBox1.Text = str1 & ":" & str2 & ":00"
答案 1 :(得分:0)
试试这段代码。它的工作原理
Dim num As Integer
num = 120 - NumericUpDown1.Value
Dim str As String = num.ToString("#000")
Dim str1 As String = "", str2 As String = ""
For i As Integer = 0 To str.Length - 1
If i = 0 Then
str1 = str(i)
Else
str2 += str(i)
End If
Next
TextBox1.Text = str1 & ":" & str2 & ":00"