2017-04-01 295 views
0

我想将总分钟转换为天数小时和分钟。我是新来的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 

当我运行它我有小数答案..任何人都可以建议我一个适当的方式做到这一点?我不想要小数。

+1

首先打开“选项严格”文本控件包含文本 - 您不能对文本进行数学运算。你还应该命名你的控件:'tbHours'对你来说比'TextBox18'更有意义。 – Plutonix

回答

4

它看起来并不像你理解变量的概念,你不需要有三个不同的处理程序来完成这项工作,你可以在一个处理程序中完成所有操作。

你可以做算术上的东西,但在我看来,更简单的方法是使用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 
+0

非常感谢你的帮助......我还有一个疑问......我有一段延迟时间..我如何将延迟时间添加到上面的代码? – APMK

+0

我试过如果然后声明,但我不能让代码工作。我想要textbox1(如上面的代码)添加延迟时间,如果提到n然后将总分钟转换为天,小时和分钟。 – APMK

+0

你是什么意思延迟时间?发布您的更新代码。 – obl

0

enter image description here

,如果我进入延时箱一定的价值我想要的日子。小时。分钟以更改为新值... @obl

+0

因此,不是“处理TextBox1.TextChanged”,而是“处理DelayTimeBox.TextChanged”。并用您的TextBox的名称替换TextBox5来显示您的输出。 – obl