|
-
May 24th, 2009, 12:54 AM
#1
Thread Starter
Lively Member
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?
-
May 24th, 2009, 01:17 AM
#2
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.
-
May 24th, 2009, 04:18 AM
#3
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.
-
May 24th, 2009, 02:37 PM
#4
Thread Starter
Lively Member
Re: Reading from a textfile and adding the contents to different listboxes
 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.
Last edited by knicksfan426; May 24th, 2009 at 02:42 PM.
-
May 24th, 2009, 04:40 PM
#5
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.
-
May 24th, 2009, 05:37 PM
#6
Thread Starter
Lively Member
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?
-
May 25th, 2009, 07:50 PM
#7
Thread Starter
Lively Member
Re: Reading from a textfile and adding the contents to different listboxes
-
May 25th, 2009, 07:57 PM
#8
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?
-
May 25th, 2009, 08:22 PM
#9
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.
-
May 25th, 2009, 09:31 PM
#10
Thread Starter
Lively Member
Re: Reading from a textfile and adding the contents to different listboxes
 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.
-
May 25th, 2009, 10:34 PM
#11
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.
-
May 26th, 2009, 02:33 PM
#12
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.
-
May 29th, 2009, 06:33 PM
#13
Thread Starter
Lively Member
Re: Reading from a textfile and adding the contents to different listboxes
 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?
Last edited by knicksfan426; May 29th, 2009 at 06:42 PM.
-
May 30th, 2009, 11:18 AM
#14
Thread Starter
Lively Member
Re: Reading from a textfile and adding the contents to different listboxes
-
May 30th, 2009, 06:03 PM
#15
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.
-
May 30th, 2009, 07:03 PM
#16
Thread Starter
Lively Member
Re: Reading from a textfile and adding the contents to different listboxes
 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.
-
May 30th, 2009, 07:44 PM
#17
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.
Last edited by minitech; May 30th, 2009 at 07:51 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|