【问题标题】:Compile error: A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll编译错误:Microsoft.VisualBasic.dll 中出现“System.InvalidCastException”类型的第一次机会异常
【发布时间】:2013-06-21 18:07:41
【问题描述】:

我在编译时在我的表单上收到此错误消息。该表单可以正常工作并且可以按我的意愿运行,但是错误不断弹出。

这是我的源代码:

Option Strict On
Option Explicit On

Public Class frmEnterMidPoint

    Dim Obj As New frmCustomRanges

    Private Sub txtMidPointValue_TextChanged(sender As Object, e As EventArgs) Handles txtMidPointValue.TextChanged

        'This event runs when the txtMidPointValue is changed.
        'it enables the clear button if it's not empty.

        If txtMidPointValue.Text = "" Then

            btnClear.Enabled = False

        Else

            btnClear.Enabled = True

        End If


    End Sub

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click


        'This event runs when the btnOk is pressed. The procedure takes the value
        'entered in the text box and converts to a decimal.  That value
        'is then sent to the frmCustomRanges form Mid label. The procedure
        'also calculates the hourly equivalent.  

        'The procedure ends by closing the current form and opening
        'the frmCustomRanges form.

        If IsNumeric(txtMidPointValue.Text) Then

            'Convert number entered to decimal
            Dim decMidPointValue As Decimal = Convert.ToDecimal(txtMidPointValue.Text)

            'Display results as dollar sign for Annual and Hourly
            Obj.lblAnnualMid.Text = decMidPointValue.ToString("C")
            Obj.lblHourlyMid.Text = Convert.ToDecimal(decMidPointValue / 52 / 40).ToString("C")

            'Close current form
            Me.Close()

            'Open frmCustomRanges
            Obj.ShowDialog()

        Else

            MsgBox("You have entered a non-numeric value. Please check value and enter again", vbCritical, "Input Error")


            With txtMidPointValue
                .Text = ""
                .Focus()

            End With

        End If

    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click

        'This event runs when the btnClear is clicked.
        'The event clears the textbox and sends the foucs
        'back to the textbox

        With txtMidPointValue
            .Text = ""
            .Focus()

        End With

    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click

        'This even runs when the btnCancel is clicked.
        'The procedure closes the current form and opens the
        'Custom Ranges form.


        'Close current form
        Me.Close()

        'Open the Custom Ranges form
        Obj.ShowDialog()


    End Sub

End Class

【问题讨论】:

  • 如果不编译,表单如何工作?
  • 我猜你的Convert.ToDecimal 之一正在抛出异常。您是否尝试将它们包装在 try/catch 中以隔离错误的来源?
  • 当您使用 IsNumeric() 而字符串不是时会发生这种情况。调试器太让你困惑了。右键单击“输出”窗口并取消选中“异常消息”。

标签: vb.net


【解决方案1】:

您可以使用 Decimal.TryParse - IsNumeric() 不一定是检查某物是否是您可以使用的数字的最佳选择。

Private Sub bnOK_Click(sender As Object, e As EventArgs) Handles bnOK.Click
    Dim decMidPointValue As Decimal
    If Decimal.TryParse(txtMidPointValue.Text, decMidPointValue) Then
        ' decMidPointValue contains the parsed value.
        Obj.lblAnnualMid.Text = decMidPointValue.ToString("C")
        Obj.lblHourlyMid.Text = (decMidPointValue / 52 / 40).ToString("C")
        ' more code...
    Else
        ' could not be parsed as a Decimal; decMidPointValue is zero.
        ' inform user.
    End If
End Sub

【讨论】:

  • 谢谢。我仍然在通过 vb.net 摸索。这很有帮助
【解决方案2】:

尝试正确转换。

Dim dec As Decimal
If Decimal.TryParse(txtMidPointValue.Text, dec)
  'dec is now converted
Else
  'dec is not converted - tell the user
End If

【讨论】:

    猜你喜欢
    • 2013-11-16
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 2012-07-18
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    相关资源
    最近更新 更多