2017-04-18 53 views
0

從某種意義上說,我的程序應該像「銀行賬戶」一樣編寫和保存信用卡和借記卡。我的問題是我的程序只是將最後輸入的行保存到文本文件中。我需要它將每個實例保存到用戶輸入文本框時的文件中。這裏是我的代碼vb.net中的文件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 = SRObject.ReadToEnd() 
    ListBox1.Items.Add(ftext) 
    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) 
    SWObject.WriteLine("Credit: " + tbDollar + ", " + tbMemo) 
    SWObject.Close() 
End Sub 

Private Sub ProcessDebit() 
    tbDollar = Tb1.Text 
    tbMemo = Tb2.Text 
    Dim SWObject As StreamWriter = New StreamWriter(fName) 
    SWObject.WriteLine("Debit: " + tbDollar + ", " + tbMemo) 
    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 

回答

0

一個StreamWriter的默認模式是覆蓋該文件。這聽起來像你想追加而不是文件。要做到這一點,通過True在構造函數中的第二個參數:

Dim SWObject As StreamWriter = New StreamWriter(fName, True) 

你需要做到這一點在你的ProcessCredit()ProcessDebit()方法兩者。

+0

我可以理解這是如何工作的,我發現它在文件中發生,但在我的列表框中,它會不斷向文件中添加文件。 I.E.,文件包含0.0,用戶輸入Credit:5.00,測試。在列表框中,它增加了「0.0 Credit:5.00,Testing」,而不是「Credit:5.00,Testing」。任何解決方案? –

+0

在你的'ShowTransactions()'方法,您正在閱讀的整個文件轉換成字符串變量'ftext',然後調用'ListBox1.Items.Add(用FText)',它增加了一個*單*項目列表框。在這種情況下,「單個項目」是整個文件。如果要將文件的每一行添加爲ListBox中的單獨條目,則需要使用循環。在循環內部調用'StreamReader.ReadLine'來讀取行和'ListBox1.Items.Add'以將其添加到列表框。您可能還想在循環前調用'ListBox1.Items.Clear()',以確保在開始添加項目之前清單已清除。 –