Write data from list box to text file

Problem referencing no. of items in list box

Page 1 of 1

1 Replies - 18849 Views - Last Post: 15 May 2009 - 09:13 PM Rate Topic: -----

#1 south73paw  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 88
  • Joined: 06-October 08

Write data from list box to text file

Post icon  Posted 15 May 2009 - 08:28 PM

Hi, now I'm up to writing user input to a text file. It's the same app as I referred to in a previous post where I was having difficulty reading data.

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)




		txtNewName.Text = ""

	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
		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)??


Is This A Good Question/Topic? 0
  • +

Replies To: Write data from list box to text file

#2 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Write data from list box to text file

Posted 15 May 2009 - 09:13 PM

The simplest way would be to reset the output file and write all the listbox entries out to the newly emptied file - safest and surest - and unless you have 100,000+ names, it is so fast as to be insignificant.

If you insist on appending only the different names, then you would need to either somehow tag the names that are already saved to avoid saving them again, or read the file in to provide a comparison table of strings not to write out. This could be screwed up by entry of repeated names.

Look at StreamWriter and File.CreateText() that overwrites any existing content in the file

This post has been edited by mark.bottomley: 15 May 2009 - 09:19 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1