Question

I need to add a validation to this code in VB. Also, Display a message box if the new balance would be a negative number. If there is not enough money to cover a check, do not deduct the check amount. Instead, display a message box with the message “Insufficient Funds” and deduct a service charge of $10.

I have no idea how to do this.

Here is the code so far:

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

Any help would be appreciated!

Was it helpful?

Solution

I'm not sure where is your problem, but I hope this will satisfy your requirements.

This line is to remove redundancy from your code, so you don't have to write validation twice:

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

The next part is to check whether he is eligible to withdraw this amount from his account or not, if not then stop him and exit the function so the GUI or DB doesn't get affected!

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

It is basiclly what you asked in the question but translated into code, nothing tricky just simple logic :))

 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top