Deleting Listbox Items?

Posts 1–9 of 9 · Page 1 of 1
Deleting Listbox Items?
I'm using this code to save/load listbox items:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim w As IO.StreamWriter
Dim r As IO.StreamReader

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
w = New IO.StreamWriter("c:\test.txt")
For i = 0 To ListBox1.Items.Count - 1
w.WriteLine(ListBox1.Items.Item(i))
Next
w.Close()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
r = New IO.StreamReader("c:\test.txt")
While (r.Peek() > -1)
ListBox1.Items.Add(r.ReadLine)
End While
r.Close()
End Sub
End Class

from:http://www.mpgh.net/forum/33-visual-...-contents.html
now i need to know how to remove the items without manually going into the text file and editing it. so i need a code for a button to remove the selected item.
Its simple :P
Code:
ListBox1.Items.Remove(ListBox1.SelectedItem)
Quote Originally Posted by nathanael890 View Post
Its simple :P
Code:
ListBox1.Items.Remove(ListBox1.SelectedItem)
but if i reload the app the item comes up again so i also need to delete it from the txt file but idont know the code
Quote Originally Posted by williamph View Post
but if i reload the app the item comes up again so i also need to delete it from the txt file but idont know the code
I just written this code today and I hope it would help you

Code:
    Public Sub RemoveItem(ByVal List As ListBox, ByVal Item As String, ByVal Filename As String)
        Try
            Dim xText As New RichTextBox With {.Text = IO.File.ReadAllText(Filename)}
            List.Items.AddRange(xText.Lines)
            List.Items.Remove(Item)
            xText.Text = ""
            For Each ITems As String In ListBox1.Items
                xText.Text &= ITems & vbCrLf
            Next
            IO.File.WriteAllText(Filename, xText.Text)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
How to use:
Code:
'Note: Make sure that the listbox has contents so you can remove it
RemoveItem(ListBox1, ListBox1.SelectedItem, "<filename>")
I'm out.. I have no more time I hope it would help you.
Or ask others if there are more easier.
Quote Originally Posted by nathanael890 View Post
I just written this code today and I hope it would help you

Code:
    Public Sub RemoveItem(ByVal List As ListBox, ByVal Item As String, ByVal Filename As String)
        Try
            Dim xText As New RichTextBox With {.Text = IO.File.ReadAllText(Filename)}
            List.Items.AddRange(xText.Lines)
            List.Items.Remove(Item)
            xText.Text = ""
            For Each ITems As String In ListBox1.Items
                xText.Text &= ITems & vbCrLf
            Next
            IO.File.WriteAllText(Filename, xText.Text)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
How to use:
Code:
'Note: Make sure that the listbox has contents so you can remove it
RemoveItem(ListBox1, ListBox1.SelectedItem, "<filename>")
I'm out.. I have no more time I hope it would help you.
Or ask others if there are more easier.
He just wants to remove a selected item, so your code is completely unnecessary, lulz.

Previous posts should work.
Quote Originally Posted by Blubb1337 View Post
He just wants to remove a selected item, so your code is completely unnecessary, lulz.

Previous posts should work.
And remove the line from the textfile too

More to the point, use this code for saving/reloading

[highlight=vb.net]
Private Sub SaveListBox(ByVal saveLoc As String, ByVal listBox As ListBox)
If Not (IO.File.Exists(saveLoc)) Then Throw New IO.FileNotFoundException("Unable to find file")
IO.File.WriteAllLines(saveLoc, listBox.Items.Cast(Of Object).Select(Function(o As Object) o.ToString()).ToArray())
End Sub

Private Sub RetrieveListBox(ByVal loadLoc As String, ByVal listBox As ListBox)
listBox.Items.Clear()
If Not (IO.File.Exists(loadLoc)) Then Throw New IO.FileNotFoundException("Unable to find file")
listBox.Items.AddRange(IO.File.ReadAllLines(loadLo c))
End Sub
[/highlight]
Quote Originally Posted by Jason View Post


And remove the line from the textfile too

More to the point, use this code for saving/reloading

[highlight=vb.net]
Private Sub SaveListBox(ByVal saveLoc As String, ByVal listBox As ListBox)
If Not (IO.File.Exists(saveLoc)) Then Throw New IO.FileNotFoundException("Unable to find file")
IO.File.WriteAllLines(saveLoc, listBox.Items.Cast(Of Object).Select(Function(o As Object) o.ToString()).ToArray())
End Sub

Private Sub RetrieveListBox(ByVal loadLoc As String, ByVal listBox As ListBox)
listBox.Items.Clear()
If Not (IO.File.Exists(loadLoc)) Then Throw New IO.FileNotFoundException("Unable to find file")
listBox.Items.AddRange(IO.File.ReadAllLines(loadLo c))
End Sub
[/highlight]
That line shouldn't show up in the textfile when resaving eh?!
Just ListBox1.Items.Remove(ListBox1.SelectedItem)
and then Re-Save the file..
already said...but this is my 2 cents...
againced blubb...not worth a brass razoo...(actuly own a brass razoo...)



top :
Code:
Imports System.IO
on form close:
Code:
Dim File As StreamWriter = File.CreateText("file.txt")
        For Each strline As String In lst.Items
            SuburbFile.WriteLine(Suburb)
        Next
        File.Close()
on remove event :
Code:
lstsuburbs.Items.Remove(lst.selecteditem)

on form load:
Code:
try
Dim File As StreamReader = File.OpenText("file.txt")
        Do While File.Peek <> -1
            lst.Items.Add(File.ReadLine())
        Loop
        File.Close()
catch ex as exeption
msgbox("file not created yet")
end try
Posts 1–9 of 9 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Talk with us