Thought I had solved the problem but oh...no
My problem is I want to delete an Item from the Listbox and therat deleting the corresponding value in the file, What am I doing wrong? Is it possible to remove entries from a file without having to delete files? What about System.IO.file.copy....?
eg:
If the items in the listbox is as follows:
Donald
Duck
Mickey
Mouse
And If I selects Mouse to remove, the data in the file look like:
Donald. (Everyone but one record is deleted)
I want it to look like this after deleting one record:
Donald
Duck
Mickey
Im inserting Items in a listbox from a textbox( txtPosts) in order to save the Items to a file. I`ve also got a button btnInsert and btnRemov.
My problem is I want to delete an Item from the Listbox and therat deleting the corresponding value in the file, What am I doing wrong? Is it possible to remove entries from a file without having to delete files? What about System.IO.file.copy....?
eg:
If the items in the listbox is as follows:
Donald
Duck
Mickey
Mouse
And If I selects Mouse to remove, the data in the file look like:
Donald. (Everyone but one record is deleted)
I want it to look like this after deleting one record:
Donald
Duck
Mickey
Im inserting Items in a listbox from a textbox( txtPosts) in order to save the Items to a file. I`ve also got a button btnInsert and btnRemov.
Visual Basic:
####Assuming Imports System.IO####
'In formloadprocedure i have the following code:
Private Sub Instillinger_Load.....
If Not File.Exists("Data.txt") Then
Dim writerAs StreamWriter = File.CreateText("Data.txt")
writer.Close()
End If
Dim ReaderAs StreamReader = File.OpenText("Data.txt")
Do While ReaderAs .Peek <> -1
ListBox1.Items.Add(leser.ReadLine)
Loop
leser.Close()
TabControl1.Dock = DockStyle.Fill
end sub
private sub Insert
Dim WriterAs StreamWriter = File.AppendText("Data.txt")
ListBox1.Items.Add(txtPosts.Text)
Writer.WriteLine(ListBox1.Items.Item(ListBox1.Items.Count - 1))
Writer.Close()
txtPosts.Text = ""
txtposts.Focus()
end sub
private sub remove
If Not File.Exists("Data.txt") Then
File.CreateText("Data.txt")
End If
Dim Writer As StreamWriter = File.CreateText("Temp.txt")
ListBox1.Items.Remove(ListBox1.SelectedItem)
Try
Writer.WriteLine(ListBox1.Items.Item(ListBox1.Items.Count - 1))
Writer.Close()
Catch x As Exception
End Try
File.Delete("Data.txt")
File.Move("Temp.txt", "Data.txt")