Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

 

####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") 

  • Leaders
Posted
On first glance, it looks like your remove method is just writing one item back to the list. The easy way would be after an item is deleted to loop through the list of remaining items, writing each one to the file. You shouldn't have to create the Temp file either.
--tim
Posted
What I thought I had done with my code was to delete one item from The listbox, then write all the remaining Items in the listbox to a file.. But here is the problem. I obviously don`t . Therefore I don`t follow your instructions. Could you demonstrate with a codeexample? :):)
  • Leaders
Posted

Just put your current writeline inside the Remove method in a loop. If this doesn't work, post your project and I'll take a look.

Dim Writer As StreamWriter = File.CreateText("Temp.txt") 
'Here you're removing the item.
       ListBox1.Items.Remove(ListBox1.SelectedItem) 
       Try 
'I think you'd be alright if you just put this in a loop of *all*
' the remaining items.  
Dim i As Integer
For i = 0 To ListBox1.Items.Count - 1
           Writer.WriteLine(ListBox1.Items.Item(i)) 
Loop
       Catch x As Exception 

       Finally
               Writer.Close() 
       End Try 

--tim

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...