如何将带有streamwriter的文本附加到文本文件中

如何将带有streamwriter的文本附加到文本文件中

本文介绍了如何将带有streamwriter的文本附加到文本文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建我的第一个应用程序(使用.vb),这是一个日历约会应用程序。我以这种方式创建了它 - 我有三个文本框,每个文本框用于日期,时间和位置。文本框中的文本条目将发送到列表框。在列表框中,我有一个保存按钮,用于将条目保存为文件夹中的文本文件。我有一个show appointmentments按钮来加载文本文件。我的问题是,当我创建一个新约会时,文本文件中的上一个条目被覆盖,所以我需要将列表框中的新条目附加到保存约会的文本文件中。



我尝试过:



这是保存按钮的代码。

 Private Sub Button3_Click(sender As Object,e As EventArgs)处理Button3.Click 
IO.Directory.CreateDirectory(C:\ Test)
Dim w As New IO.StreamWriter(C:\ Test \ 01.txt)
Dim i As Integer

For i = 0 To ListBox1.Items.Count - 1
w .WriteLine(ListBox1.Items.Item(i))
下一个
w.Close()
End Sub





这是整个应用程序代码。

 Public Class Form1 
Private Sub Button1_Click(sender As Object,e As EventArgs)Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
ListBox1.Items.Add(Te xtBox2.Text)
ListBox1.Items.Add(TextBox3.Text)
End Sub

Private Sub Button2_Click(sender As Object,e As EventArgs)处理Button2.Click
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex())
End Sub

Private Sub ListBox1_SelectedIndexChanged(sender As Object,e As EventArgs)处理ListBox1.SelectedIndexChanged,ListBox1.Click


End Sub

Private Sub Button3_Click(sender As Object,e As EventArgs)处理Button3.Click
IO.Directory.CreateDirectory(C:\\ \\ Test)
Dim w As New IO.StreamWriter(C:\Test\01.txt)
Dim i As Integer

For i = 0 To ListBox1.Items.Count - 1
w.WriteLine(ListBox1.Items.Item(i))
Next
w.Close()
End Sub

Private Sub Button4_Click(发送者作为对象,e作为EventArgs)处理Button4.Click
Dim r作为IO.StreamReader
r =新的IO.StreamReader(C:\ Test \ 01.txt)
while(r.Peek()> -1)
ListBox1.Items.Add(r.ReadLine)
End
r.Close()
End Sub

Private Sub Button5_Click( sender As Object,e As EventArgs)处理Button5.Click
Me.TextBox1.Text = Me.DateTimePicker1.Text

End Sub
End Class
解决方案

I'm building my first application (using .vb), which is a calendar appointment app. I've created it in this manner - I have three textboxes, one each for date, time and location. The text entries from the textboxes are sent to a list box. At the listbox, I have a save button to save the entries as an appointment to a text file in a folder. I have a show appointments button to load the text file. My problem is that when I create a new appointment, the previous entry on the text file is overwritten, so I need to append the new entries in the list box to the text file saving the appointments.

What I have tried:

This is the code for the save button.

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        IO.Directory.CreateDirectory("C:\Test")
        Dim w As New IO.StreamWriter("C:\Test\01.txt")
        Dim i As Integer

        For i = 0 To ListBox1.Items.Count - 1
            w.WriteLine(ListBox1.Items.Item(i))
        Next
        w.Close()
    End Sub



This is the entire app code.

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ListBox1.Items.Add(TextBox1.Text)
        ListBox1.Items.Add(TextBox2.Text)
        ListBox1.Items.Add(TextBox3.Text)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ListBox1.Items.RemoveAt(ListBox1.SelectedIndex())
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged, ListBox1.Click


    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        IO.Directory.CreateDirectory("C:\Test")
        Dim w As New IO.StreamWriter("C:\Test\01.txt")
        Dim i As Integer

        For i = 0 To ListBox1.Items.Count - 1
            w.WriteLine(ListBox1.Items.Item(i))
        Next
        w.Close()
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim r As IO.StreamReader
        r = New IO.StreamReader("C: \Test\01.txt")
        While (r.Peek() > -1)
            ListBox1.Items.Add(r.ReadLine)
        End While
        r.Close()
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Me.TextBox1.Text = Me.DateTimePicker1.Text

    End Sub
End Class
解决方案


这篇关于如何将带有streamwriter的文本附加到文本文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 20:52