Reading from a textfile and adding the contents to different listboxes
I have a form with 2 listboxes. I've added items in each listbox. I save all the items from each listbox to a text file. What I want to do is open the text file and I want each string inside the text file to go to the appropriate listbox. How can I make my program recognize where each string belongs?
Re: Reading from a textfile and adding the contents to different listboxes
Use some sort of flag when saving, something that would never be in either listbox. For example:
vb Code:
'Save routine
Dim sw As New IO.StreamWriter(filename)
Dim i As Integer = 0
While i < Me.ListBox1.Items.Count
sw.WriteLine(CStr(Me.ListBox1.Items(i)))
i+=1
End While
i = 0
sw.WriteLine("[SEPARATORSTRING]")
While i < Me.ListBox2.Items.Count
sw.WriteLine(CStr(Me.ListBox2.Items(i)))
i+=1
End While
sw.Close()
And do the opposite to save. Ignore that one line and switch listboxes after it.
Re: Reading from a textfile and adding the contents to different listboxes
I would suggest using separate files.
In fact, you don't even really need files at all. You could add a StringCollection to your application settings for each ListBox and then the data will be stored in the config file for you.
Re: Reading from a textfile and adding the contents to different listboxes
Quote:
Originally Posted by
minitech
Use some sort of flag when saving, something that would never be in either listbox. For example:
vb Code:
'Save routine
Dim sw As New IO.StreamWriter(filename)
Dim i As Integer = 0
While i < Me.ListBox1.Items.Count
sw.WriteLine(CStr(Me.ListBox1.Items(i)))
i+=1
End While
i = 0
sw.WriteLine("[SEPARATORSTRING]")
While i < Me.ListBox2.Items.Count
sw.WriteLine(CStr(Me.ListBox2.Items(i)))
i+=1
End While
sw.Close()
And do the opposite to save. Ignore that one line and switch listboxes after it.
I switched your code around a little bit but I worked off of it. Thank you. Check out the code, I have a little problem
Save File code:
VB .NET Code:
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
SaveFileDialog1.Filter = ".txt (*.txt)|*.txt"
SaveFileDialog1.DefaultExt = "txt"
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Using myWriter As New System.IO.StreamWriter(SaveFileDialog1.FileName)
myWriter.WriteLine("|")
For Each Item As String In ListBox1.Items
myWriter.WriteLine(Item)
Next
myWriter.WriteLine("||")
For Each Item As String In ListBox2.Items
myWriter.WriteLine(Item)
Next
End Using
End If
End Sub
Open File code:
VB .NET Code:
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
OpenFileDialog1.Filter = ".txt (*.txt)|*.txt"
OpenFileDialog1.DefaultExt = "txt"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim myReader As New System.IO.StreamReader(OpenFileDialog1.FileName)
If myReader.ReadLine = "|" Then
Do Until myReader.ReadLine = "||"
ListBox1.Items.Add(myReader.ReadLine)
Loop
End If
End If
End Sub
As you can see, I've used the separator "|" for the listbox1 items and "||" for listbox2 items. In my open file coding, whenever I run the program, VB gives me an error on line 12 saying "Value cannot be null. Parameter name: item." What do I do about this? I can't find any way to properly work around it.
Re: Reading from a textfile and adding the contents to different listboxes
One of the lines will be blank if you use .WriteLine(). Just add "On Error Resume Next" at the beginning of your Sub.
Re: Reading from a textfile and adding the contents to different listboxes
Alright, I did what you said and the code now looks like this
Open File code:
vb Code:
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
On Error Resume Next
OpenFileDialog1.Filter = ".txt (*.txt)|*.txt"
OpenFileDialog1.DefaultExt = "txt"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim myReader As New System.IO.StreamReader(OpenFileDialog1.FileName)
If myReader.ReadLine = "|" Then
Do Until myReader.ReadLine = "||"
ListBox1.Items.Add(myReader.ReadLine)
Loop
End If
End If
End Sub
I still get an error in "Immediate Window" that says "A first chance exception of type 'System.ArgumentNullException' occurred in System.Windows.Forms.dll" What do I do?
Re: Reading from a textfile and adding the contents to different listboxes
Re: Reading from a textfile and adding the contents to different listboxes
That's not an error, or at least not one you have to worry about. A message about a "first chance exception" means that an exception has been thrown, which is perfectly legal. It when an exception isn't caught that you have to worry. Does the application keep running and do what it's supposed to do?
Re: Reading from a textfile and adding the contents to different listboxes
First chance exceptions don't really seem to mean much. Just ignore them if you see that in the Debug window.
Re: Reading from a textfile and adding the contents to different listboxes
Quote:
Originally Posted by
jmcilhinney
That's not an error, or at least not one you have to worry about. A message about a "first chance exception" means that an exception has been thrown, which is perfectly legal. It when an exception isn't caught that you have to worry. Does the application keep running and do what it's supposed to do?
No, the application freezes.
Re: Reading from a textfile and adding the contents to different listboxes
I just noticed that you have On Error Resume Next in your code. Get rid of that and never use it again. Now your code will crash and we can see what the actual issue. Then we can fix it.
Re: Reading from a textfile and adding the contents to different listboxes
1. Try taking out the OpenFileDialog1.DefaultExt line.
2. Make sure the program has permissions to the file.
3. Take out On Error Resume Next if it still doesn't work.
Re: Reading from a textfile and adding the contents to different listboxes
Quote:
Originally Posted by
minitech
1. Try taking out the OpenFileDialog1.DefaultExt line.
2. Make sure the program has permissions to the file.
3. Take out On Error Resume Next if it still doesn't work.
I did everything you said, my application still freezes. To everyone, here's the open file dialog code:
Note: Please remember the "|" is the separator I have made that comes before all listbox1 items and "||" is the separator that comes before all listbox2 items.
.NET Code:
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
OpenFileDialog1.Filter = ".txt (*.txt)|*.txt"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim myReader As New System.IO.StreamReader(OpenFileDialog1.FileName)
If myReader.ReadLine = "|" Then
Do Until myReader.ReadLine = "||"
ListBox1.Items.Add(myReader.ReadLine)
Loop
End If
End If
End Sub
End Class
At the line of code where it says "ListBox1.Items.Add(myReader.ReadLine)", VB says there is an error there: "Value cannot be null.Parameter name: item" What do I do to fix this?
Re: Reading from a textfile and adding the contents to different listboxes
Re: Reading from a textfile and adding the contents to different listboxes
Change Do Until myReader.ReadLine = "||" To Do Until (myReader.Peek() = -1) OrElse (myReader.ReadLine = "||").
See if that fixes the problem.
Re: Reading from a textfile and adding the contents to different listboxes
Quote:
Originally Posted by
minitech
Change Do Until myReader.ReadLine = "||" To Do Until (myReader.Peek() = -1) OrElse (myReader.ReadLine = "||").
See if that fixes the problem.
There is improvement but it is minimal. The file is opened with out any errors now but only part of the file is being opened, not all text is being distributed to the proper listboxes.
Re: Reading from a textfile and adding the contents to different listboxes
That's because you haven't added a loop for the second listbox.
After your first loop, add:
vb.net Code:
While myReader.Peek > -1
Me.ListBox2.Items.Add(myReader.ReadLine())
End While
By the way, if you haven't already, check your file; It needs to look like this:
file Code:
|
List Box 1 Item 1
List Box 1 Item 2
||
List Box 2 Item 1
List Box 2 Item 2
List Box 3 Item 3
If it doesn't, I have no idea what the problem is.