2017-04-01 165 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