我想做一个简单的"总工作时间";计算器。
我有一个文本框,我想写入,比如,1.30
,它将是1小时30分钟。我有另一个文本框,我将把相同的1.30
,然后我想添加一个标签,并显示3小时。
然而,我找不到正确的代码将数字转换为小时,而是显示2.6。我怎样才能让它显示3小时呢?
我的代码
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label1.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = My.Settings.SaveTextBox()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
My.Settings.SaveTextBox = TextBox1.Text()
End Sub
End Class
使用NumericUpDown
将节省您必须检查用户是否真的在文本框中输入了一个数字。在属性窗口中,将NumericUpDownMinutes
设置为最多59。PadLeft
将前导零添加到个位数分钟。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Hours = NumericUpDownHours.Value.ToString
Dim Minutes = NumericUpDownMinutes.Value.ToString
lblTotal.Text = $"{Hours}.{Minutes.PadLeft(2, "0"c)}"
End Sub
获取四舍五入的小时数
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim Hours = NumericUpDownHours.Value
Dim Minutes = NumericUpDownMinutes.Value
Dim RoundedHours = CInt(Hours + Minutes / 60)
lblRoundedTotal.Text = RoundedHours.ToString
End Sub