The following code works, but it's not really the scenario I'm after.
Private Sub btnWriteToFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteToFile.Click Dim newName As String = txtNewName.Text Dim sw As StreamWriter = New StreamWriter("MyFile.txt", True) lstFileLoaded.Items.Add(newName) sw.WriteLine(newName) sw.Close() txtNewName.Text = "" txtNewName.Focus() End Sub
Note: Imports System.IO is written at the top of the code
In the sense that this code works, it goes by the above assumption...
1. User enters new name in text box
2. When button is clicked, name is added to list box (appended to data already there from text file) AND new name is added to the text file.
My question is this - how can I make it that a user has the ability to add as many new names as they want to the list box, but then save ALL of the names to the text file in one hit. Having a button for 'Add' and another one for 'Save' is what I'm thinking about here.
When I tried to save the contents of a list box back to the text file, I hit a problem.
For i As Integer = 0 To lstFileLoaded.Items.Count - 1 sw.WriteLine(newName) Next i
Instead of adding the new name to the text file, a list of numbers were added instead. I think it's got something to do with the first line here and 'count'. Let's say I had 3 names already in the file and added a 4th name. After writing to the file, the text file reads:
How do I get it so that the new name is appended and not a count of the number of names (including the new one)??