2015-11-20 52 views
1

我有一個簡單的VB程序,它通過TextBox.Enter累積失敗。目標是:如何修復TextBox.Enter?我沒有收到MsgBox,表明沒有遵循行動樹。VB textbox.enter給出空值(累加器)

Option Explicit On 
Public Class MainForm 

Public decexpenses, decincome As Decimal 
Public dectotalexpenses As Decimal = 0 
Public dectotalincome As Decimal = 0 

Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    MsgBox("Use Enter key to enter values.") 
End Sub 

Private Sub expensesTextBox_Enter(sender As Object, e As EventArgs) Handles expensesTextBox.Enter 
    Do Until expensesTextBox.Text = String.Empty 
     Dim expenses = expensesTextBox.Text 
     MsgBox(expenses) ' i dont get a msgbox for this indicating this value is null 
     Do Until IsNumeric(expenses) 
      MsgBox("Please enter numeric value for expenses.") 
     Loop 
     decexpenses = CDec(expenses) 
     dectotalexpenses = decexpenses + dectotalexpenses 
    Loop 
End Sub 

Private Sub incomeTextBox_Enter(sender As Object, e As EventArgs) Handles incomeTextBox.Enter 
    Do Until incomeTextBox.Text = String.Empty 
     Dim income = incomeTextBox.Text 
     Do Until IsNumeric(income) 
      MsgBox("Please enter numeric value for income.") 
     Loop 
     decincome = CDec(income) 
     dectotalincome = decincome + dectotalexpenses 
    Loop 
End Sub 

End Class 
+0

在大多數情況下,當用戶去/激活/進入文本框的內容是空的,所以外面的'Do Until'條件已經滿足,什麼也沒有發生。好東西,因爲內部的'Do Until'是MsgBoxes的無限風雪 – Plutonix

+0

你是什麼意思? –

+0

您在輸入任何內容之前正在進行驗證。在離開事件中驗證或使用驗證事件。你不能在事件中使用這樣的循環 - 你必須退出事件,以便他們可以輸入/更正輸入 – Plutonix

回答

1

難道你不正確的方法,做到以下?看起來您的所有目光都是檢查以確定它是一個輸入到文本框中的整數,並且如果它計算它,否則顯示一個消息框以告訴用戶輸入一個數字。

Private Sub expensesTextBox_Enter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles expensesTextBox.KeyPress 
    Dim dectotalexpenses As Decimal = 0 
    Dim dectotalincome As Decimal = 0 

    Dim income As String = expensesTextBox.Text 

    If Keys.E + Keys.Enter Then 


     If IsNumeric(income) = True Then 
      dectotalincome = income + dectotalexpenses 

     Else 
      MessageBox.Show("Please Enter A Number") 
     End If 
    End If 
End Sub