1

嘿伙计们,我的程序一直有这个崩溃问题。

我正在尝试创建一个程序,它通过输入他们的期初余额、信用额度、总费用和总信用来计算一个人的账户余额。在这种情况下,我在总费用和总积分代码方面遇到了具体问题。

我设置了一个消息框,如果“总费用”和“总积分”框为空白,它将显示“请输入 .... 的数值”。问题是当我运行它并输入一个空白时,会出现消息然后程序崩溃。

崩溃后,该程序然后以黄色突出显示特定的转换代码(在这种情况下:decTotalCharges = Convert.ToDecimal(txtTotalCharges.Text))并且错误显示Input string was not in correct format.

怎么回事,我把它转换成正确的格式了吗?(十进制到十进制?)。以下是对我的代码的深入了解:

Public Class frmEndingBalance

'Declare module level variables
Dim mdecEndingBalance As Decimal
Dim mdecAllCharges As Decimal
Dim mdecAllCredits As Decimal
Dim mintCustomersOverLimit As Integer

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    'clears the form
    'clears the labels
    txtAccountNumber.Text = ""
    txtBeginningBalance.Text = ""
    txtTotalCharges.Text = ""
    txtTotalCredits.Text = ""
    txtCreditLimit.Text = ""
    lblEndingBalance.Text = ""
    lblCreditMessage.Text = ""
    lblAllCharges.Text = ""
    lblAllCredits.Text = ""
    lblCustomersOverLimit.Text = ""
    lblCreditMessage.Text = ""

    'clear the textboxes
    txtAccountNumber.Clear()
    txtBeginningBalance.Clear()
    txtTotalCredits.Clear()
    txtTotalCharges.Clear()
    txtCreditLimit.Clear()

    'clear module level variables
    mdecEndingBalance = 0
    mdecAllCharges = 0
    mdecAllCredits = 0
    mintCustomersOverLimit = 0
End Sub

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

    'Declare Variables
    Dim intAccountNumber As Integer
    Dim intBeginningBalance As Integer
    Dim decTotalCharges As Decimal
    Dim decTotalCredits As Decimal
    Dim decCreditLimit As Decimal
    Dim mdecEndingBalance As Decimal 'Beginning Balance + Charges - Credits()
    Dim decCreditMessage As Decimal

    'check for numeric
    If IsNumeric(txtAccountNumber.Text) = False Then 'value is not numeric
        MessageBox.Show("You can't enter anything blank!")
        Exit Sub
    End If

    'convert to a numeric data type
    intAccountNumber = Convert.ToInt32(txtAccountNumber.Text)

    'check for numeric
    If String.IsNullOrEmpty(txtBeginningBalance.Text) Then MessageBox.Show("Beginning Balance Cannot Be Blank!")

    'check for everything else
    If IsNumeric(txtBeginningBalance.Text) = False Then 'Value is not numeric
        MessageBox.Show("Please enter a numeric value for Beginning Balance!")
        Exit Sub
    End If
    'convert to a numeric data type
    intBeginningBalance = Convert.ToInt32(txtBeginningBalance.Text)

    'check for numeric
    If IsNumeric(txtTotalCharges.Text) = False Then 'value is not numeric
        MessageBox.Show("Please enter a numeric value for Total Charges!")
    End If

    'convert 
    decTotalCharges = Convert.ToDecimal(txtTotalCharges.Text)

    'check for 0 or positive
    If decTotalCharges < 0 Then
        MessageBox.Show("Please enter a positive value or zero for number of Total Charges!")
        Exit Sub
    End If

    'check for numeric
    If IsNumeric(txtTotalCredits.Text) = False Then 'value is not numeric 
        MessageBox.Show("Please enter a numeric value for Total Credits")
    End If

    'convert to a numeric data type
    decTotalCredits = Convert.ToDecimal(txtTotalCredits.Text)


    'check for 0 or positive
    If decTotalCredits < 0 Then
        MessageBox.Show("Please enter a positive value or zero for total credits!")
    End If

    'check numeric
    If IsNumeric(txtCreditLimit.Text) = False Then 'value is not numeric
        MessageBox.Show("Please enter a numeric value for the Credit Limit!")
    End If

    'convert to a numeric data type
    decCreditLimit = Convert.ToDecimal(txtCreditLimit.Text)


    'check for a 0 or positive
    If decCreditLimit < 0 Then
        MessageBox.Show("Please enter a positive value or zero for Credit Limit!")
    End If

    'check for customers over limit
    decCreditMessage = decCreditLimit - (mdecEndingBalance)

    'running totals
    mdecAllCharges += decTotalCharges
    mdecAllCredits += decTotalCredits

    'calculate Ending Balance
    mdecEndingBalance = Convert.ToDecimal(intBeginningBalance + decTotalCharges - (decTotalCredits))

    'display outputs
    lblEndingBalance.Text = mdecEndingBalance.ToString("c")
    lblAllCharges.Text = mdecAllCharges.ToString("c")
    lblAllCredits.Text = mdecAllCredits.ToString("c")
    lblCustomersOverLimit.Text = mintCustomersOverLimit.ToString("n0")
End Class

任何帮助将不胜感激!

谢谢

4

1 回答 1

0

您在测试中错过了 Exit Sub

If IsNumeric(txtTotalCharges.Text) = False Then 'value is not numeric
 MessageBox.Show("Please enter a numeric value for Total Charges!")
End If

你在其他几个地方也做过(我们都做过,事实上我们也做过:()。值得用一个小辅助方法或类似的东西进行重构。

If IsValidDecimal(txtTotalCharges.Text, "Total Charges") 
  ' etc
End If
于 2012-09-23T21:23:22.833 回答