|
-
Apr 13th, 2008, 01:11 AM
#1
Thread Starter
Lively Member
[RESOLVED][2008]FileWriteline how read write listbox to file?
this is interesting again, i have been trying this for the past 6 hours without zero progress..
So my question does vb have this at all, becaus eI searched & I cant find nothing that i can make to work at all?
Code:
filewriteline(filename,"text",linenr)
filereadline (filename,"text",linenr)
basically i have a listbox & i want to save all listbox items into txt file so i can load them back again lather. well this is what i have & its not really working:
this is how I load items from txt file to listbox But sometimes its working(putting item on next listbox line) & sometimes not its like magic really..:
Code:
Dim file_name As String = load_listbox
Dim stream_reader As New IO.StreamReader(file_name)
ListBox1_include.Items.AddRange(Split(stream_reader.ReadToEnd, _
vbCrLf))
ListBox1_include.SelectedIndex = 0
stream_reader.Close()
& this is how I write the listbox items to file:
Code:
' For nr = 1 To ListBox1_include.Items.Count - 1
' 'write all items into txt file
' My.Computer.FileSystem.WriteAllText(links_include, ListBox1_include.Items.Item(nr) & Chr(13), True)
' Next
Last edited by goldenix; Apr 13th, 2008 at 08:57 AM.

M.V.B. 2008 Express Edition
-
Apr 13th, 2008, 02:25 AM
#2
Re: File Write line how?
Code:
Public Sub ReadFile(ByVal fileName As String, ByVal listBox As ListBox)
If IO.File.Exists(fileName) Then
listBox.Items.Clear()
Dim stream As New IO.FileStream(fileName, IO.FileMode.Open, IO.FileAccess.Read)
Dim reader As New IO.StreamReader(stream)
listBox.Items.AddRange(Split(reader.ReadToEnd, vbNewLine))
reader.Close()
stream.Close()
End If
End Sub
Public Sub WriteFile(ByVal fileName As String, ByVal listBox As ListBox)
Dim stream As New IO.FileStream(fileName, IO.FileMode.CreateNew, IO.FileAccess.Write)
Dim writer As New IO.StreamWriter(stream)
For Each line As String In listBox.Items
writer.WriteLine(line)
Next
writer.Close()
stream.Close()
End Sub
-
Apr 13th, 2008, 08:31 AM
#3
Thread Starter
Lively Member
Re: File Write line how?
thanx its finally working, After some more hours thinking I cme to conclusion, that my problem was that in the 4 listboxes I have, there is 1 first entry by default & it must stay there & also the empty lines were hard to handle, well hire is the final working code if someone needs it:
I this example we have 2 listboxes:
ListBox1_include
ListBox2_ignore_text
Code:
'=======================================
' Declaring Listbx load/write at startup
'=======================================
Dim links_include As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\links_include.txt"
Dim ignore_text As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\ignore_text.txt"
Dim load_listbox
Dim write_listbox
Dim write_listbox_file
Dim read_listbox_file
Code:
'----------------------------
' Write listbox data to files
' First we assign values for each listbox
' so listbox & file to write to = proper
'----------------------------
Dim a As Integer
For a = 1 To 2
If a = 1 Then
write_listbox = ListBox1_include
write_listbox_file = links_include
End If
If a = 2 Then
write_listbox = ListBox2_ignore_text
write_listbox_file = ignore_text
End If
_write_listbox_tofile()
Next
Code:
'=============================================
' we call this same function for each listbox to
' write all listboxes contents to files
'=============================================
Sub _write_listbox_tofile()
If IO.File.Exists(write_listbox_file) Then IO.File.Delete(write_listbox_file)
Dim stream As New IO.FileStream(write_listbox_file, IO.FileMode.CreateNew, IO.FileAccess.Write)
Dim writer As New IO.StreamWriter(stream)
For Each line As String In write_listbox.Items
writer.WriteLine(line)
Next
writer.Close()
stream.Close()
End Sub
Code:
'----------------------------
' Fill in Listboxes With data
' First we assign values for each listbox
' so listbox & file to read from = proper
'----------------------------
For a = 1 To 4
If a = 1 Then
load_listbox = ListBox1_include
read_listbox_file = links_include
End If
If a = 2 Then
load_listbox = ListBox2_ignore_text
read_listbox_file = ignore_text
End If
_load_listbox()
Next
End Sub
Code:
'======================================
' Fill in Listboxes With data @ startup
' Again we call this same function for
' Each listbox to avoid writing too much code
'======================================
Sub _load_listbox()
If IO.File.Exists(read_listbox_file) Then
'ListBox1_include.Items.Clear()
Dim stream As New IO.FileStream(read_listbox_file, IO.FileMode.Open, IO.FileAccess.Read)
Dim reader As New IO.StreamReader(stream)
load_listbox.Items.AddRange(Split(reader.ReadToEnd, vbNewLine))
' Remove last item from the listbox because its empty line
load_listbox.Items.RemoveAt(load_listbox.Items.Count - 1)
' Remove first listbox item cuz I want to
load_listbox.Items.RemoveAt(0)
reader.Close()
stream.Close()
End If
End Sub
Last edited by goldenix; Apr 13th, 2008 at 08:35 AM.

M.V.B. 2008 Express Edition
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
|