|
-
May 24th, 2009, 01:17 AM
#1
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, 02:37 PM
#2
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
#3
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.
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
|