2017-02-15 94 views
0

這裏我有一個基本的銀行賬戶,允許用戶輸入他們的借方和貸方金額以及每筆金額的備忘錄。用戶也可以顯示所有交易及其餘額。我無法顯示所有交易,也無法創建一個方程式來減去信用借方。這是迄今爲止我所擁有的,因爲我已經堅持了幾個小時。從文本框中存儲多個用戶輸入並從文本框中添加多個值?

Public Class Form1 
    Dim valueDebit As Decimal 
    Dim valueCredit As Decimal 
    Dim total As Decimal 



    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     Me.Text = "Mike Smith's Bank Account" 
     Lb1.Visible = False 
     Lb2.Visible = False 
     Lb3.Visible = False 
     Tb1.Visible = False 
     Tb2.Visible = False 
     Bt1.Visible = False 
    End Sub 

    Private Sub Cb1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Cb1.SelectedIndexChanged 
     If Tb1.Text = "" Then 
      Tb1.Text = "0.00" 

     ElseIf Cb1.Text = "Credit" Then 
      Lb1.Visible = True 
      Lb2.Visible = True 
      Lb3.Visible = False 
      Tb1.Visible = True 
      Tb2.Visible = True 
      Lb1.Text = "Enter Credit Amount" 
      Lb2.Text = "Describe the Income" 
      Bt1.Visible = True 

      valueCredit = Convert.ToDecimal(Tb1.Text) 



     ElseIf Cb1.Text = "Debit" Then 
      Lb1.Visible = True 
      Lb2.Visible = True 
      Lb3.Visible = False 
      Tb1.Visible = True 
      Tb2.Visible = True 
      Lb1.Text = "Enter Debit Amount" 
      Lb2.Text = "Describe the Expense" 
      Bt1.Visible = True 

      valueDebit = Convert.ToDecimal(Tb1.Text) 


     ElseIf Cb1.Text = "Display Transactions" Then 
      Lb1.Visible = False 
      Lb2.Visible = False 
      Lb3.Visible = True 
      Tb1.Visible = False 
      Tb2.Visible = False 
      Bt1.Visible = False 


     ElseIf Cb1.Text = "Display Balance" Then 
      Lb1.Visible = False 
      Lb2.Visible = False 
      Lb3.Visible = True 
      Tb1.Visible = False 
      Tb2.Visible = False 
      Lb3.Text = "$" 

     End If 
    End Sub 

    Private Sub Bt1_Click(sender As Object, e As EventArgs) Handles Bt1.Click 
     Cb1.Text = "" 
     Tb1.Visible = False 
     Tb2.Visible = False 
     Lb1.Visible = False 
     Lb2.Visible = False 
     Tb1.Text = "" 
     Tb2.Text = "" 

    End Sub 
End Class 

任何幫助,將不勝感激。

回答

0

首先,借記是收入和信用是銀行賬戶的支出。 其次,我認爲提交類型的按鈕將在這種情況下理想

我試過的一個選項:使公共類交易,然後使用列表(交易)列表和交易列表框上的表單。還使用「提交」交易按鈕。然後,您可以使用循環獲取交易清單並獲取總金額/餘額。

Public Class Form1 
    'Mod-level variable 
    Dim TransactionsList As New List(Of Transaction)  

    'Not exactly sure what all the labels are and the exact layout of your form, 
    'so you can apply this to your code 
    Private Sub Cb1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Cb1.SelectedIndexChanged 
    If Tb1.Text = "" Then 
     Tb1.Text = "0.00" 

    ElseIf Cb1.Text = "Credit" Then 
     Lb1.Visible = True 
     Lb2.Visible = True 
     Lb3.Visible = False 
     Tb1.Visible = True 
     Tb2.Visible = True 
     Lb1.Text = "Enter Credit Amount" 
     Lb2.Text = "Describe the Income" 
     Bt1.Visible = True 

     'valueCredit = Convert.ToDecimal(Tb1.Text) (Handled in buttonClick event) 

    ElseIf Cb1.Text = "Debit" Then 
     Lb1.Visible = True 
     Lb2.Visible = True 
     Lb3.Visible = False 
     Tb1.Visible = True 
     Tb2.Visible = True 
     Lb1.Text = "Enter Debit Amount" 
     Lb2.Text = "Describe the Expense" 
     Bt1.Visible = True 

     'valueDebit = Convert.ToDecimal(Tb1.Text) (Handled in buttonClick event) 

    ElseIf Cb1.Text = "Display Transactions" Then 
     Lb1.Visible = False 
     Lb2.Visible = False 
     Lb3.Visible = True 
     Tb1.Visible = False 
     Tb2.Visible = False 
     Bt1.Visible = False 

     'This is where you loop through list to get your transactions 
     For Each transaction In TransactionList 
      'Add each Transaction to the ListBox. Display the data however you like... 
      TransactionsListBox.Items.Add(
        String.Format("{0}: {1:C}. {2}", 
           transaction.Type, 
           transaction.Amount, 
           transaction.Description)) 
     Next  

    ElseIf Cb1.Text = "Display Balance" Then 
     Lb1.Visible = False 
     Lb2.Visible = False 
     Lb3.Visible = True 
     Tb1.Visible = False 
     Tb2.Visible = False 
     Lb3.Text = "$" 

    End If 
    End Sub  

    'Click button to submit the transaction.. 
    'Also easier for data validation, error catching, etc. 
    Private Sub SubmitTransactionButton_Click(sender As Object, e As EventArgs) Handles SubmitTransactionButton.Click 
     Dim transactionTypeString As String = Cb1.Text 
     Dim transactionDescriptionstring As String = Tb2.Text 
     Dim transactionAmountDecimal As Decimal 

     'If user enters non-numeric value, exception gets thrown 
     Try 
      'Convert amount to Decimal 
      transactionAmountDecimal = Decimal.Parse(Tb1.Text)   

      'Add to the list of Transactions 
      TransactionList.Add(New Transaction( 
          transactionTypeString, 
          transactionAmountDecimal, 
          transactionDescriptionstring)) 

     Catch FormatExceptionParameter as FormatException 
      transactionAmountDecimal = 0D 

     End Try 


    End Sub 

End Class 


Public Class Transaction 
    Public Sub New(type As String, amount As Decimal, description As String) 
     Me.Type = type 
     Me.Amount = amount 
     Me.Description = description  

     'Optional depending on how you want to enter data 
     'Ex. Enters 500 as a Credit (should be -500 to balance) (500 * -1 = -500)    
     If Me.Type = "Credit" Then 
      Me.Amount *= -1 
     End If 

    End Sub 

    Public Property Type As String 
    Public Property Amount As Decimal 
    Public Property Description As String 

End Class 

然後用上面的計算...

'Create Calculate function 
Public Function CalculateBalance() As Decimal 
     Dim balanceDecimal As Decimal 

     'Looping through TransactionsList again to get each Amount, 
     'this time to calculate total Balance 
     For Each transaction In TransactionList 
      balanceDecimal += transaction.Amount 

     Next 

     Return balanceDecimal 

End Function 

呼叫在顯示器的天平否則/ if塊

ElseIf Cb1.Text = "Display Balance" Then 
      Lb1.Visible = False 
      Lb2.Visible = False 
      Lb3.Visible = True 
      Tb1.Visible = False 
      Tb2.Visible = False 
      Lb3.Text = "$" 

      'Whatever control you want to display the balance in 
      Tb1.Text = CalculateBalance().ToString("C2") 

    End If 

有,你可以使用許多可能的解決方案,並這是其中的一個,你也可以從中得到一些其他的想法。我希望我幫你找到了你想要做的事情。

相關問題