我在我的節目一個糟糕的錯誤:如果一個用戶按下檢查(計算)按鈕時出現在文本框中的程序顯示此錯誤代碼輸入:「從字符串轉換‘’鍵入'Double'是無效的。「我想解決這個問題,但我不確定如何進行轉換。我在想可能是CType,但我正在聽說解析。我如何去做這件事?該文本框稱爲mskTxtInput,該按鈕對象稱爲btnCheck,它執行所有計算和處理。VB.Net從字符串轉換爲加倍
更新:這是我的代碼,除了解析方法,所以希望這有助於一點!
私人小組btnCheck_Click(BYVAL發件人爲System.Object的,BYVALË作爲System.EventArgs)把手btnCheck.Click pic1.Visible =假「隱藏圖片 pic1.Image = My.Resources.A pic2.Image = My.Resources.F
Dim value As Double
If Double.TryParse(mskTxtInput.Text, value) = Then
MsgBox("parsing success") ' parsing worked, so use the value in here
Else
MsgBox("parsing failed") ' parsing failed, so alert the user to that fact
End If
If radAdd.Checked = True Then
totalNum = num1 + num2
End If
If radSub.Checked = True Then
totalNum = num1 - num2
End If
If radMulti.Checked = True Then
totalNum = num1 * num2
End If
If mskTxtInput.Text = totalNum Then
lblAns.Text = ("Correct!")
lblAns2.Text = ("Answer is " & totalNum)
pic1.Visible = True
wins = wins + 1
nScore = wins
Else
lblAns.Text = ("Incorrect")
lblAns2.Text = ("Answer should be " & totalNum)
pic2.Visible = True
End If
attempts = attempts + 1
If attempts = 5 Then
MessageBox.Show("Game Finished! ", "End Of Game", _
MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
lblAns.Text = ("You scored " & wins & " Out of 5")
btnSpin.Enabled = False
pic1.Visible = False
pic2.Visible = False
lblAns2.Text = ""
lblAns2.Text = "Play again?"
btnCheck.Enabled = False
btnNew.Enabled = True
attempts = 0
wins = 0
End If
mskTxtInput.Clear()
mskTxtInput.Focus()
End Sub
請分享一些代碼中發生此問題。 – BeemerGuy 2010-11-18 08:30:49
這與您的問題沒有直接關係,但看起來您並未使用Option Strict。我建議你打開它:然後編譯器會生成有關危險代碼的警告,從長遠來看可以節省您的時間。 – MarkJ 2010-11-18 11:17:49