You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB程序输入非数值内容时崩溃问题求助

Fix: VB.NET Form Crashing on Non-Numeric Input

Hey there, I spot the exact issue right away—your code is trying to convert input values to integers before entering the Try...Catch block. So when someone types non-numeric text, the CInt conversion throws an unhandled exception before your error handling can kick in, causing the crash. Let's fix this with two solid solutions:

Approach 1: Quick Fix – Move Conversions Inside the Try Block

This is the simplest tweak—just relocate the CInt calls inside your Try block so any conversion errors get caught by the Catch:

Public Class Form1
    Private Sub BtnCalculateRevenue_Click(sender As Object, e As EventArgs) Handles BtnCalculateRevenue.Click
        Dim intvalue As Integer
        Dim intvalue2 As Integer
        Dim intvalue3 As Integer
        Dim total As Double

        Try
            LblStatus.Text = String.Empty
            ' Move conversion logic inside the Try block to catch errors
            intvalue = CInt(TxtInputClassA.Text)
            intvalue2 = CInt(TxtInputClassB.Text)
            intvalue3 = CInt(TxtinputClassC.Text)

            LblClassAResult.Text = (intvalue * 15).ToString("c")
            LblClassBResult.Text = (intvalue2 * 12).ToString("c")
            LblClassCResult.Text = (intvalue3 * 9).ToString("c")
            total = CDbl((intvalue * 15) + (intvalue2 * 12) + (intvalue3 * 9))
            LblTotal.Text = total.ToString("c")
        Catch
            LblStatus.Text = "Please Enter a Valid Number in All Fields"
        End Try
    End Sub

    Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
        Me.Close()
    End Sub

    Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
        TxtInputClassA.Clear()
        TxtInputClassB.Clear()
        TxtinputClassC.Clear()
        LblClassAResult.Text = String.Empty
        LblClassBResult.Text = String.Empty
        LblClassCResult.Text = String.Empty
        LblTotal.Text = String.Empty
        LblStatus.Text = String.Empty ' Clear status message too
    End Sub
End Class

Instead of relying on exception handling for invalid inputs, Integer.TryParse is more efficient and lets you give users specific feedback about which field has an issue:

Public Class Form1
    Private Sub BtnCalculateRevenue_Click(sender As Object, e As EventArgs) Handles BtnCalculateRevenue.Click
        Dim classA As Integer
        Dim classB As Integer
        Dim classC As Integer
        Dim total As Double

        LblStatus.Text = String.Empty

        ' Validate each input one by one
        If Not Integer.TryParse(TxtInputClassA.Text, classA) Then
            LblStatus.Text = "Please enter a valid number for Class A"
            TxtInputClassA.Focus()
            Return
        End If

        If Not Integer.TryParse(TxtInputClassB.Text, classB) Then
            LblStatus.Text = "Please enter a valid number for Class B"
            TxtInputClassB.Focus()
            Return
        End If

        If Not Integer.TryParse(TxtinputClassC.Text, classC) Then
            LblStatus.Text = "Please enter a valid number for Class C"
            TxtinputClassC.Focus()
            Return
        End If

        ' All inputs are valid—calculate and display results
        LblClassAResult.Text = (classA * 15).ToString("c")
        LblClassBResult.Text = (classB * 12).ToString("c")
        LblClassCResult.Text = (classC * 9).ToString("c")
        total = (classA * 15) + (classB * 12) + (classC * 9)
        LblTotal.Text = total.ToString("c")
    End Sub

    Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
        Me.Close()
    End Sub

    Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
        TxtInputClassA.Clear()
        TxtInputClassB.Clear()
        TxtinputClassC.Clear()
        LblClassAResult.Text = String.Empty
        LblClassBResult.Text = String.Empty
        LblClassCResult.Text = String.Empty
        LblTotal.Text = String.Empty
        LblStatus.Text = String.Empty
    End Sub
End Class

Why Integer.TryParse is Better:

  • It doesn't throw exceptions, making your code faster and more stable.
  • You can guide users directly to the problematic input field, improving their experience.
  • It avoids a "catch-all" Catch block that might hide other unexpected errors.

内容的提问来源于stack exchange,提问作者Wolffpup

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 20:48:14