我正在寫一個簡單的應用程序,它將2個文本框的輸入作爲分子和分母,然後將它們傳遞給Reduce()
方法。爲什麼2個Try/Catch塊會凍結應用程序?
我需要將文本框字符串轉換爲整數,但我不希望它停止程序,如果用戶意外鍵入非數字鍵,所以我把任務放在Try ... Catch語句中爲了防止這種情況發生。
但是,當我在2個文本框中鍵入字母並按下按鈕時,窗口就會凍結並且全部扭曲。任何人都可以解釋發生了什麼?
這是我的代碼:
Private Sub btnReduce_Click(ByVal sender As Object,
ByVal e As EventArgs) Handles btnReduce.Click
Dim n As Integer
Dim d As Integer
Try
n = Val(txtNum.Text)
Catch ex As Exception
MsgBox("Please enter a numeric numerator", , "ERROR")
Exit Sub
End Try
Try
d = Val(txtDenom.Text)
Catch ex As Exception
MsgBox("Please enter a numeric denominator", , "ERROR")
Exit Sub
End Try
Reduce(n, d)
Dim reduced As String = n.ToString + "/" + d.ToString
lblDisplay.Text = "The reduced fraction is " + reduced
End Sub
非常感謝! :)過去幾個月你對我的幫助很大。 –