如何添加验证?

时间:2014-03-03 23:25:01

标签: vb.net validation .net-4.0

我需要在VB中为此代码添加验证。此外,如果新余额为负数,则显示消息框。如果没有足够的钱支付支票,请不要扣除支票金额。而是显示一个消息框,其中显示消息“资金不足”,并扣除10美元的服务费。

我不知道该怎么做。

以下是目前的代码:

Public Class CheckingForm
Private BalanceDecimal As Decimal

Private Sub CalculateTextBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateTextBox.Click
    'Calculate the transaction and display the new balance
    Dim AmountDecimal As Decimal

    If DepositRadioButton.Checked Or CheckRadioButton.Checked Or ChargeRadioButton.Checked Then
        Try
            AmountDecimal = Decimal.Parse(AmountTextBox.Text)

            If DepositRadioButton.Checked = True Then
                BalanceDecimal += AmountDecimal
            ElseIf CheckRadioButton.Checked = True Then
                BalanceDecimal -= AmountDecimal
            ElseIf ChargeRadioButton.Checked = True Then
                BalanceDecimal -= AmountDecimal
            End If

            BalanceTextBox.Text = BalanceDecimal.ToString("C")
        Catch AmountException As FormatException
            MessageBox.Show("Please make sure that only numeric data has been entered.",
                "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            With AmountTextBox
                .Focus()
                .SelectAll()
            End With
        Catch AnyException As Exception
            MessageBox.Show("Error: " & AnyException.Message)
        End Try
    Else
        MessageBox.Show("Please select deposit, check, or service charge", "Input needed")
    End If
End Sub

Private Sub ClearTextBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearTextBox.Click
    'Clear the form

    DepositRadioButton.Checked = False
    ChargeRadioButton.Checked = False
    CheckRadioButton.Checked = False
    With AmountTextBox
        .Clear()
        .Focus()
    End With
End Sub

Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
    'End the program

    Me.Close()
End Sub

Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintButton.Click
    'Print the form

    PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
    PrintForm1.Print()
End Sub

Private Sub CheckingForm_Load(ByVal sender As System.Object, ByVal e As    System.EventArgs) Handles MyBase.Load

End Sub
End Class

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

我不确定你的问题在哪里,但我希望这能满足你的要求。

这一行是为了从代码中删除冗余,因此您不必编写两次验证:

ElseIf CheckRadioButton.Checked = True OR ChargeRadioButton.Checked = True Then

接下来的部分是检查他是否有资格从他的帐户中提取这笔款项,如果没有,则停止他并退出该功能,以免GUI或DB受到影响!

IF (BalanceDecimal - AmountDecimal > 0 ) THEN
    BalanceDecimal -= AmountDecimal
ELSE
    'SHOW YOUR MESSAGE HERE
     EXIT

这基本上是你在问题中提出的但是转化为代码,没有什么棘手的简单逻辑:))

 Private Sub CalculateTextBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateTextBox.Click
        'Calculate the transaction and display the new balance
        Dim AmountDecimal As Decimal

        If DepositRadioButton.Checked Or CheckRadioButton.Checked Or ChargeRadioButton.Checked Then
            Try
                AmountDecimal = Decimal.Parse(AmountTextBox.Text)

                If DepositRadioButton.Checked = True Then
                    BalanceDecimal += AmountDecimal
                ElseIf CheckRadioButton.Checked = True OR ChargeRadioButton.Checked = True Then
                    IF (BalanceDecimal - AmountDecimal > 0 ) THEN
                        BalanceDecimal -= AmountDecimal
                    ELSE
                        'SHOW YOUR MESSAGE HERE
                         EXIT
                    END IF
                END IF

                BalanceTextBox.Text = BalanceDecimal.ToString("C")
            Catch AmountException As FormatException
                MessageBox.Show("Please make sure that only numeric data has been entered.",
                    "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                With AmountTextBox
                    .Focus()
                    .SelectAll()
                End With
            Catch AnyException As Exception
                MessageBox.Show("Error: " & AnyException.Message)
            End Try
        Else
            MessageBox.Show("Please select deposit, check, or service charge", "Input needed")
        End If
    End Sub