繁体   English   中英

vb.net中的文件IO问题

[英]Issue with File IO in vb.net

从某种意义上说,我的程序应该像“银行帐户”一样编写和保存贷方和借方。 我的问题是我的程序仅将最后输入的行保存到文本文件中。 我需要它来将每个实例保存到用户在文本框中输入输入内容时的文件。 这是我的代码

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

StreamWriter的默认模式是覆盖文件。 听起来您想附加到文件。 为此,将True作为构造函数中的第二个参数传递:

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

您需要同时在ProcessCredit()ProcessDebit()方法中执行此操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM