2014-03-03 44 views
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 

任何幫助,將不勝感激!

+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 

這是basiclly你在問問題,但是翻譯成代碼,沒有什麼棘手的只是簡單的邏輯:))

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 
+2

通常指導用戶指向正確的方向,而不是僅僅爲他們傾銷代碼會好得多。代碼答案在這裏是不鼓勵的。只需提供代碼,除了如何複製和粘貼外,什麼都不教。至少要提供一些解釋你的答案的文字,而不是僅僅爲他們傾銷代碼。在過程中教*某事*。 –

+0

你是對的!我認爲代碼是自我解釋的,但下次我會。 謝謝您的輸入Ken – user2517028

+0

現在還不算太晚。你可以[編輯你的答案](http://stackoverflow.com/posts/22159890/edit)並立即修復它。 :-) –