将总分钟转换为天数,小时数,分钟数vb
问题描述:
我想将总分钟转换为天数小时和分钟。我是新来的VB和仍然努力学习...... 这是我的代码...将总分钟转换为天数,小时数,分钟数vb
Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
TextBox1.Text = TextBox3.Text/1440
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
TextBox3.Text = TextBox1.Text * 1440
TextBox7.Text = TextBox1.Text/24
End Sub
Private Sub TextBox7_TextChanged(sender As Object, e As EventArgs) Handles TextBox7.TextChanged
TextBox1.Text = TextBox7.Text * 24
End Sub
当我运行它我有小数答案..任何人都可以建议我一个适当的方式做到这一点?我不想要小数。
答
它看起来并不像你理解变量的概念,你不需要有三个不同的处理程序来完成这项工作,你可以在一个处理程序中完成所有操作。
你可以做算术上的东西,但在我看来,更简单的方法是使用TimeSpan。这将是这个样子:
'TextBox1 is where the user inputs the number of minutes
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim d = TimeSpan.FromMinutes(TextBox1.Text) 'creates a TimeSpan based on user input, this is also a variable creation
TextBox5.Text = d.ToString("dd\:hh\:mm") 'you can format it with a custom format
'Or you can access the elements of the TimeSpan like so
TextBox2.Text = d.Days
TextBox3.Text = d.Hours
TextBox4.Text = d.Minutes
End Sub
答
,如果我进入延时箱一定的价值我想要的日子。小时。分钟以更改为新值... @obl
+0
因此,不是“处理TextBox1.TextChanged”,而是“处理DelayTimeBox.TextChanged”。并用您的TextBox的名称替换TextBox5来显示您的输出。 – obl
首先打开“选项严格”文本控件包含文本 - 您不能对文本进行数学运算。你还应该命名你的控件:'tbHours'对你来说比'TextBox18'更有意义。 – Plutonix