Question How do I Re-Write a File Once a Item is Deleted in the list View?

kiddashy

Member
Joined
Nov 10, 2012
Messages
6
Programming Experience
Beginner
I Have a List View and a Save Button Once you add items into the list view and click save, it saves to the text file and then you can reopen and display in the listview. But once you delete a item from the list view and save it, it does not delete the selected item form the textfile so when you open it everything is still there? How do I Re-Write the File or Deleted the Selected item from the file??

I Have a btnDelete which Deletes the Selected item in the list view.
And i Have a Save Button Which Uses the Stream Reader and Writer.
 

kiddashy

Member
Joined
Nov 10, 2012
Messages
6
Programming Experience
Beginner
Sorry About that, Here is the Code that Handles BtnDelete_Click:

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
'Deletes The Seleted or Multi Seleted Items in the List View
For Each SeletedItem As ListViewItem In lsviEmployeeDetails.SelectedItems
lsviEmployeeDetails.Items.Remove(SeletedItem)

Next
End Sub
 

22-degrees

Well-known member
Joined
Feb 11, 2012
Messages
156
Location
South East Ireland
Programming Experience
1-3
OK well your delete code does what it is supposed to do..

How do you transfer the listview items in order to use

For Each Employee As AddEmployee In EmployeeList


because I don't see a reference to the lsviEmployeeDetails ListView in your write code..
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
Why do so many people make things harder for themselves by using a ListView? You could have used an XML file, a DataTable and a DataGridView and you'd have needed less than 10 lines of code total to load, add, edit, delete and save.

Anyway, your btnDelete code is removing the item from the ListView but when you save you are using a list of AddEmployee objects in EmployeeList. Where's the code to remove items from that?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
You shouldn't use a ListView in the first place. You should simply bind your list to a DataGridView. Then you only have to delete from one list. If you're determined to use a ListView then you should assign the original object to the Tag property of the ListViewItem that it corresponds to. That way, whenever you do something with the ListViewItem you can easily access the corresponding original entity and perform the corresponding action.
 
Top Bottom