从文本框存储多个用户输入并从文本框添加多个值?

时间:2017-02-15 04:00:14

标签: vb.net

这里我有一个基本的银行账户,允许用户输入他们的借方和贷方金额以及每个金额的备忘录。用户还可以显示所有交易及其余额。我无法显示所有交易,也无法创建一个等式来减去信用额。这是我到目前为止所做的,因为我已经坚持了好几个小时。

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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

首先,借方是收入,贷方是银行账户的开支。 其次,我认为提交类型的Button在这种情况下是理想的

我尝试过的一个选项:制作公共类交易,然后在表单上使用List(of Transaction)和事务列表框。还使用按钮“提交”交易。然后,您可以使用循环来获取交易清单并获得总金额/余额。

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

在显示余额中调用else / if block

    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

您可以使用许多解决方案,而这也是您可以从中获得其他一些想法的解决方案之一。我希望我帮助你找到你想要做的事情。