2017-02-15 101 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 

有,你可以使用许多可能的解决方案,并这是其中的一个,你也可以从中得到一些其他的想法。我希望我帮你找到了你想要做的事情。

相关问题