Hey Have a real problem here, I am making a simple calculator and it's saying InvalidCastException was unhandled when I try to debug it, here's the code, please tell me whats wrong with it: (and yes I have renamed the textboxes to addequals etc...

Public Class Form1

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    addequals.Text = Val(add1.Text) + Val(add2.Text)
    subtractequals.Text = Val(subtract1.Text) - (subtract2.Text)
    multiplyequals.Text = Val(multiply1.Text) * (multiply2.Text)
    divideequals.Text = Val(divide1.Text) / (divide2.Text)
End Sub

End Class

Which line is throwing the error?
MSDN says an InvalidCastException can be thrown when you try a conversion from a Single or a Double to a Decimal, the source value is infinity, Not-a-Number (NaN), or too large to be represented as the destination type.
Could it be the divide part can't handle a decimal value or you have a zero in there as a start value?

Should your code not be something like subtractequals.Text = Val(subtract1.text) - Val(subtract2.Text)? Instead of subtractequals.Text = Val(subtract1.text) - (subtract2.Text)? In otherwords, you are telling the code the first textbox text is a numeric value and then are subtracting the second textbox text (which is a string...)

This article has been dead for over six months. Start a new discussion instead.