我的程序本质上是一个小型的银行帐户"允许用户将借方和贷记写入帐户(文本文件)。目前,我的程序将允许用户执行此类操作,并在名为"显示余额"的组合框索引中计算余额。虽然此程序将从余额中减去借方并将余额添加到余额中,但它也应该能够获取现有文件,从中读取余额,保存,等等。
我的问题是当我的程序在文件打开时更新余额时,它不会将余额保存回程序,然后读取余额,显示在"显示余额" index,然后允许用户继续更新它。
我已经尝试添加StreamReader以在选择show balance index时从文件中读取余额,然后将余额写回文件,但我似乎无法使其正常工作。 这完全是我想要的:该计划目前将把所有的债务和信用写入文件,但我需要一种方式来控制"文件中的平衡。我需要阅读资产余额中的平衡()来自文件,然后可以更改使用的债务和信用,并将其写回到文件,因此可以再次使用。当前平衡设置为默认为0,并且只能在程序打开时进行修改,并且一旦终止并重新启动,平衡将仍然显示为0.这是什么需要固定。 以下是我的代码:
Imports System.IO
Public Class Form1
Dim tbDollar As String
Dim tbMemo As String
Dim fName As String
Dim creditAmount As String
Dim debitAMount As String
Dim Balance As Double
Dim action As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Text = "Mike Smith's Bank Account"
Call LoadData()
Call MainMenu()
End Sub
Private Sub Cb1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Cb1.SelectedIndexChanged
action = Cb1.SelectedIndex
If action = 0 Then
Call SetUpCredit()
ElseIf action = 1 Then
Call SetUpDebit()
ElseIf action = 2 Then
Call ShowTransactions()
ElseIf action = 3 Then
Call ShowBalance()
End If
End Sub
Private Sub btConfirm_Click(sender As Object, e As EventArgs) Handles btConfirm.Click
If action = 0 Then
Call ProcessCredit()
Balance = Balance + Convert.ToDouble(tbDollar)
ElseIf action = 1 Then
Call ProcessDebit()
Balance = Balance - Convert.ToDouble(tbDollar)
End If
Cb1.SelectedIndex = -1
Tb1.Text = ""
Tb2.Text = ""
Lb4.Text = Convert.ToString(Balance)
Lb1.Visible = False
Lb2.Visible = False
Tb1.Visible = False
Tb2.Visible = False
ListBox1.Visible = False
btConfirm.Visible = False
End Sub
Private Sub MainMenu()
btConfirm.Visible = False
Lb1.Visible = False
Lb2.Visible = False
Tb1.Visible = False
Tb2.Visible = False
ListBox1.Visible = False
Lb4.Visible = False
End Sub
Private Sub LoadData()
Dim FileFound = False
Do Until FileFound = True
fName = InputBox("Please enter your file path", "Enter your file path")
If File.Exists(fName) Then
FileFound = True
Else
MessageBox.Show("File not found!", "Error", MessageBoxButtons.OK)
End If
Loop
End Sub
Private Sub SetUpCredit()
btConfirm.Visible = True
Lb1.Visible = True
Lb2.Visible = True
Tb1.Visible = True
Tb2.Visible = True
ListBox1.Visible = False
Lb4.Visible = False
Lb1.Text = "Enter Credit Amount"
Lb2.Text = "Enter a Memo"
End Sub
Private Sub SetUpDebit()
btConfirm.Visible = True
Lb1.Visible = True
Lb2.Visible = True
Tb1.Visible = True
Tb2.Visible = True
ListBox1.Visible = False
Lb4.Visible = False
Lb1.Text = "Enter Debit Amount"
Lb2.Text = "Enter a Memo"
End Sub
Private Sub ShowTransactions()
btConfirm.Visible = False
Lb1.Visible = False
Lb2.Visible = False
Tb1.Visible = False
Tb2.Visible = False
ListBox1.Visible = True
Lb4.Visible = False
Dim SRObject As StreamReader = New StreamReader(fName)
Dim ftext As String
Do While SRObject.Peek() <> -1
ftext = SRObject.ReadLine()
If ListBox1.Items.Contains(ftext) Then
Else
ListBox1.Items.Add(ftext)
End If
Loop
SRObject.Close()
End Sub
Private Sub ShowBalance()
btConfirm.Visible = False
Lb1.Visible = False
Lb2.Visible = False
Tb1.Visible = False
Tb2.Visible = False
ListBox1.Visible = False
Lb4.Visible = True
Lb4.Text = "$" + Convert.ToString(Balance)
End Sub
Private Sub ProcessCredit()
tbDollar = Tb1.Text
tbMemo = Tb2.Text
Dim SWObject As StreamWriter = New StreamWriter(fName, True)
SWObject.WriteLine("Credit: " + tbDollar + ", " + tbMemo)
MessageBox.Show("You credited " + tbDollar + " to your account!", "Success!")
SWObject.Close()
End Sub
Private Sub ProcessDebit()
tbDollar = Tb1.Text
tbMemo = Tb2.Text
Dim SWObject As StreamWriter = New StreamWriter(fName, True)
SWObject.WriteLine("Debit: " + tbDollar + ", " + tbMemo)
MessageBox.Show("You debited" + tbDollar + "to your account!", "Success!")
SWObject.Close()
End Sub
Private Sub Tb1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Tb1.KeyPress
If Not Char.IsDigit(e.KeyChar) Then e.Handled = True
If e.KeyChar = "," And Tb1.Text.IndexOf(",") = -1 Then e.Handled = False
If e.KeyChar = Chr(8) Then e.Handled = False
If e.KeyChar = "." And Tb1.Text.IndexOf(".") = -1 Then e.Handled = False
If e.KeyChar = Chr(13) Then Tb2.Focus()
End Sub
End Class
I believe there should be a streamreader in the show balance sub to read the first line of the file and save it as the balance, and then write the balance back to the user when it is updated, but i cannot seem to get it figured out. Any help would be appreciated.