2016-04-21 1525 views
0

我寫了一個完美的代碼,只要用戶輸入正確的信息。如果用戶在文本框中輸入數字,我會收到一條錯誤消息,告知用戶必須輸入數據。發生這種情況時,我得到一個轉換異常:無效轉換 - 拋出異常:'System.InvalidCastException'

拋出異常:在Microsoft.VisualBasic.dll中則「」

其他信息:從字符串「」轉換爲類型「整數」是無效的。

我是新來的,我希望有任何建議。

例外發生在這條線:

intDetermineSubTotalCost(intSiteSelected, CInt(txtNumberOfNights.Text)) 

如果我刪除嚴格的選項,它會與預期的談話工作,但是該項目要求它的。

Dim intSiteSelected As Double 
Dim intNumberOfNights As Integer 
Dim blnNightsStayingIsVaild As Boolean = False 
Dim blnDiscountIsSelected As Boolean = False 
Dim intDiscountChoice As Integer 
Dim strDiscountSelected As String = "" 

' Call a function to confirm if the Number of Nights staying is valid. 
blnNightsStayingIsVaild = ValidateNightsStaying() 

' call a function to confirm a discount has been seected. 
intDiscountChoice = ValidateSelectedDiscount(blnDiscountIsSelected, strDiscountSelected) 

' If number of nights staying and available discount has been selected, 
' calculate the subtotal, tax, and final cost. 
If (blnNightsStayingIsVaild And blnDiscountIsSelected) Then 
    intNumberOfNights = Convert.ToInt32(txtNumberOfNights.Text) 
    intSiteSelected = Me.cmbSelectASite.SelectedIndex 
End If 


If cmbSelectASite.SelectedIndex = 0 Then 
    intSiteSelected = 20D 
ElseIf cmbSelectASite.SelectedIndex = 1 Then 
    intSiteSelected = 35D 
ElseIf cmbSelectASite.SelectedIndex = 2 Then 
    intSiteSelected = 55D 
End If 

intDetermineSubTotalCost(intSiteSelected, CInt(txtNumberOfNights.Text)) 


Private Function ValidateNightsStaying() As Boolean 

' This function validate the value entered for the number of nights staying in campground. 
Dim intNightsStaying As Integer 
Dim blnValidityCheck As Boolean = False 
Dim StrNumberOfNightsErrorMessage As String = 
    "Please Enter The Number Of Nights You Are Staying (1-99)" 
Dim strMessageBoxTitle As String = " You Must Choose Number Of Nights" 

Try 
    intNightsStaying = Convert.ToInt32(txtNumberOfNights.Text) 
    If intNightsStaying > 0 And intNightsStaying < 100 Then 
     blnValidityCheck = True 
    Else 
     MsgBox(StrNumberOfNightsErrorMessage, , strMessageBoxTitle) 
     blnValidityCheck = True 
    End If 
Catch Exception As FormatException 
    MsgBox(StrNumberOfNightsErrorMessage, , strMessageBoxTitle) 
    txtNumberOfNights.Focus() 
    txtNumberOfNights.Clear() 
Catch execption As OverflowException 
    MsgBox(StrNumberOfNightsErrorMessage, , strMessageBoxTitle) 
    txtNumberOfNights.Focus() 
    txtNumberOfNights.Clear() 
Catch execption As SystemException 
    MsgBox(StrNumberOfNightsErrorMessage, , strMessageBoxTitle) 
    txtNumberOfNights.Focus() 
    txtNumberOfNights.Clear() 
End Try 
Return blnValidityCheck 

End Function 
+0

試試這個http://www.dotnetperls.com/parse-vbnet – Claudius

+0

是什麼'intDetermineSubTotalCost'?錯誤本身是因爲空字符串('「」')不能轉換爲整數。對於所有的用戶輸入使用Integer.TryParse,所以你可以處理,當他們輸入什麼愚蠢 – Plutonix

回答

2

有幾種方法可以解決這個問題。

  1. 您可以檢查字符串是否爲空並驗證。
  2. 將轉換更改爲TryParse,IIRC將在空的時候返回0
  3. 如果可能,請將輸入從文本框更改爲數字上/下控件,最小值和最大值分別爲1和99。
+0

嘿所有,我試圖改變驗證部分中的代碼,並且我不斷得到相同的結果。我可以假設的最好的是,我的問題是這條線。 intDetermineSubTotalCost(intSiteSelected,CInt(txtNumberOfNights.Text))。這一行是爲了計算選定網站的成本並乘以晚上的文本框數量而寫的。 –

2

你可以看看這樣的事情,這將限制他們進入非數字按鍵。

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress 

'97 - 122 = Ascii codes for simple letters 
'65 - 90 = Ascii codes for capital letters 
'48 - 57 = Ascii codes for numbers 

If Asc(e.KeyChar) <> 8 Then 
    If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then 
     e.Handled = True 
    End If 
End If 

End Sub 
1

只是在你進行計算之前做一些輸入檢查。 例如文本框,要求輸入:

 If txtInput.text.Contains("whatever you don't want there") Then 
      MessageBox.Show("Please Enter The Correct Info") 
      txtInput.Focus() 
      Return 
     End if 

或者

 If Integer.TryParse(txtInput.Text, input) Then 
      MessageBox.Show("Please Enter Some Text.") 
      txtInput.Focus() 
      Return 
     End If 

這將停止代碼崩潰,如果有一個誤差,併爲用戶的機會,以固定它們的輸入。一旦所有的輸入已經被檢查,執行你的計算。