I've put a list view in my program which operates fine, but if I check two items, they aren't removed from the file. I can't seem to understand why as I have used a for each to remove them. Here is the code snippet.
Private Sub btnUnbook_Click(sender As System.Object, e As System.EventArgs) Handles btnUnbook.Click
Dim IndexOfChoice As String 'Index of the item a user has chosen
Dim MakeNewFile As New System.IO.StreamWriter(CurDir() & "\NewFile.csv") 'Make new file is a writer used to write every line apart from the unbooked facilities
Dim CollectOldFile As New System.IO.StreamReader(PCFile) 'Collect old file is a reader
Dim FileLineText As String 'File line text holds the current line
Dim CurrentIndex As Integer 'Current index is the item that the program is checking
For Each SelectedItems As ListViewItem In lstFacilities.CheckedItems 'For each checked item (Dimmed as Selected item to make it simpler)
IndexOfChoice = SelectedItems.Index 'Index of selected item is put into a variable
lstFacilities.Items.Remove(SelectedItems) 'Remove current item from display
FileLineText = "" 'Blank file line text
CollectOldFile.Close() 'Closes file reader and writer to avoid a second choice crashing the program
MakeNewFile.Close()
CollectOldFile = New IO.StreamReader(PCFile) 'Declares the writer and reader as new reader/writers
MakeNewFile = New IO.StreamWriter(CurDir() & "\NewFile.csv")
Do 'Begins a do loop
FileLineText = CollectOldFile.ReadLine 'Hold read line in a variable
If FileLineText = Nothing Then 'If nothing found
Exit Do
Else 'Otherwise
If CurrentIndex <> IndexOfChoice Then 'If this is not the selected item
MakeNewFile.WriteLine(FileLineText) 'Write to new file
End If
End If
CurrentIndex += 1 'Increment the index
Loop Until FileLineText = Nothing
CollectOldFile.Close() 'Close reader and writer
MakeNewFile.Close()
System.IO.File.Delete(PCFile) 'Delete outdated file
My.Computer.FileSystem.RenameFile(CurDir() & "\NewFile.csv", "PCFile.csv") 'Rename as PC file
Next
MsgBox("The facilitiy selected has been unbooked.", , "Unbooking Successful") 'Inform the user
End Sub
In addition, how do you make a message box appear if there are no checked items?
Thanks,
Bryn.

New Topic/Question
Reply



MultiQuote





|