Question Deleting Items from Listbox Logically (From file)

Shyamz1

Member
Joined
Feb 5, 2010
Messages
9
Programming Experience
Beginner
Hi there, I'm trying to delete an item from a listbox so that it wouldn't appear when i run the program again. Is there anyway you could do this.

Note that i am an amateur at VB so take it easy lol.

Regards

Shyam
 
I'm new too :)

I've found that ListBox1.Items.Clear() clears all items

ListBox1.Items.Remove(0) also works, where 0 is the first in the list.

Hope this helps
Johno
 
Yeah, but i want to know how to delete it logically from a file, so once you start up the program again it dleetes it from the listbox but is still in file if you get me. because deleting a record permanently is impossible unless you delete the whole file...

Regards
 
When the program exits you could have it write the data back out to the file (overwriting the file).

That way when the program runs again all the data is there, except the deleted item that is.
 
To answer your question in any worthwhile manner we need to know how you are filling your ListBox (from a database, text file, something else?).
 
Will one line of code work. I am trying to just delete a record from the listbox and over-write the file. So is this the code i need?
 
What it is , is that i have a dat file. and the records are retrieved from there. so do i put this code below the private sub of the btn delete click?
 
What it is , is that i have a dat file. and the records are retrieved from there. so do i put this code below the private sub of the btn delete click?

Please explain the problem better. Describe the purpose of the app, what it is doing. All I could find on dat files on the internet is below from Online Tech Tips:
The first thing to understand about .DAT files is that it indicates a file that has arbitrary data. That means it’s not associated with any one particular program or application.
So saying records are retrieved from a dat file doesn't tell me much. Perhaps showing the relevant code you have so far would clear things up.
 
Hi, This is what i have so far. It's a basic program into which your names and age are entered followed by clicking an input button which stores it in the file. Then i click another button which lists the records into the listbox. What i'm trying to do is delete a record so it doesn't appear in the listbox even if i run the program again.

VB.NET:
Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click
    oneitem.Forename = txtForename.Text
    oneitem.Surname = txtSurname.Text
    oneitem.Age = txtAge.Text

    FileClose(1)
    FileOpen(1, Filename, OpenMode.Random, , , Len(OneItem))
    NumberofRecords = LOF(1) / Len(OneItem)
    FilePut(1, OneItem, NumberofRecords + 1)
    NumberofRecords = NumberofRecords + 1
End Sub


Private Sub btnList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
    Dim Index As Integer

    NumberofRecords = LOF(1) / Len(OneItem)
    For Index = 1 To NumberofRecords
        FileGet(1, OneItem, Index)

        lstItems.Items.Add(String.Format(MyFormat, _
                OneItem.Forename, OneItem.Surname, OneItem.Age))


        'the code above displays items (records) in a list box

    Next Index   ' the file gets the next record in file and this process is repeated 
                 ' until end of records or no records are found
    FileClose()  'the file Clients.dat is closed
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Filename = "Items.dat"

End Sub
 
The code you show is kind of outdated. I would recommend using an XML type data storage.

I'm not sure how to use the file seek method. I would read the text file in to a string variable, populate the list with that. Then when the changes are to be saved, write the string variable out to the text file using the code I previously provided.
 
Back
Top