Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 29-Jun-17 3:18am
v3
Comments
KarstenK 29-Jun-17 9:22am    
It is not a good idea to use a directory on C: directly. Get some User directory for that use.

Tip: use better variable names to enhance code quality. ;-)

1 solution

Use the append parameter set to true, see the MSDN page;

StreamWriter Constructor (String, Boolean, Encoding, Int32) (System.IO)[^]

Dim w As New IO.StreamWriter("C:\Test\01.txt", True)
 
Share this answer
 
Comments
Member 13279041 14-Jul-17 9:16am    
Thank You.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900