Vb.net 从文本框中存储多个用户输入并从文本框中添加多个值?
在这里,我有一个基本的银行账户,允许用户输入他们的借方和贷方金额以及每个金额的备忘录。用户还可以显示所有交易及其余额。我在显示所有交易以及创建从贷项中减去借项的等式时遇到问题。这是我到目前为止所做的,因为我已经被困在这个问题上好几个小时了Vb.net 从文本框中存储多个用户输入并从文本框中添加多个值?,vb.net,Vb.net,在这里,我有一个基本的银行账户,允许用户输入他们的借方和贷方金额以及每个金额的备忘录。用户还可以显示所有交易及其余额。我在显示所有交易以及创建从贷项中减去借项的等式时遇到问题。这是我到目前为止所做的,因为我已经被困在这个问题上好几个小时了 Public Class Form1 Dim valueDebit As Decimal Dim valueCredit As Decimal Dim total As Decimal Private Sub Form1_
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
任何帮助都将不胜感激。首先,借记是收入,贷记是银行账户上的支出。 其次,我认为在这种情况下,提交类型按钮是理想的 我尝试过的一个选项是:创建公共类事务,然后在表单上使用(事务的)列表和事务列表框。还可以使用“提交”事务的按钮。然后,您可以使用循环来获取事务列表并获取总金额/余额
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
调用显示Balance else/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
你可以使用很多解决方案,这是其中之一,你也可以从中获得一些其他想法。我希望我能帮你找到你想要做的事