基本上我想要一個文本框來做到這一點:
TextBox5.Text = TextBox4.Text / TextBox2.Text
它可以作業,但如果我不嘗試在文本框中輸入內容,它不會更新。我如何能 ?
uj5u.com熱心網友回復:
您可以同時處理 TextBox_TextChanged 事件,并嘗試將字串決議為雙精度。如果兩者都成功,則計算并更新 TextBox。
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
doMath()
End Sub
Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged
doMath()
End Sub
Private Sub doMath()
Dim tb2 As Double, tb4 As Double
If Double.TryParse(TextBox2.Text, tb2) AndAlso Double.TryParse(TextBox4.Text, tb4) Then
TextBox5.Text = (tb4 / tb2).ToString()
Else
TextBox5.Text = ""
End If
End Sub
當其他兩個文本框都是有效的雙精度時,文本框將立即用商更新。
請記住不要嘗試使用字串進行數學運算。您可能希望放在Option Strict On代碼檔案的頂部,以便編譯器告訴您不能用這樣的字串進行數學運算TextBox4.Text / TextBox2.Text
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/490859.html
