question

WilfredEghenedji-9495 avatar image
0 Votes"
WilfredEghenedji-9495 asked WilfredEghenedji-9495 commented

Delete SelectedItem in Listbox


 Private Sub BtnBibleVerseMsgBox2Okay_Click(sender As Object, e As EventArgs) Handles BtnBibleVerseMsgBox2Okay.Click
          If ListBox1.SelectedIndex <> -1 Then
             System.IO.File.Delete(ListBox1.Items(ListBox1.SelectedIndex).fullname())
             ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
         End If
 End Sub

Please, I am intending to delete Listbox item i.e. delete selected item from both folder and Listbox using the above code. However, implementing the code, the file was deleted accordingly, but i got a conflicting error with a previous code, as given below, at line 13:

 Dim files() As String
 Dim Pause As Integer = 5
 Dim pointer As Integer = 0
 Dim folder As String = "ph1\"  ' set to required folder
    
 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         files = IO.Directory.GetFiles(folder, "*.txt")
         Timer2.Enabled = True
 End if
    
 Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
         Timer2.Interval = Pause * 1000
         LblBibleVerses3.Text = IO.File.ReadAllText(files(pointer))
         pointer += 1
         If pointer > files.Count - 1 Then pointer = 0
 End Sub

This terminated the application.

Solution Attempt:

I have attempted some solutions i.e. using the below code, added in Timer2_Tick event, but all to no avail, see below:

 If LblBibleVerses3.Text Is Nothing Then
     Return
 End If

Please, see attachment to view the error page. Thank you.!

[1]: /answers/storage/attachments/197129-capture-20220427-111452new.png











dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LesHay-2099 avatar image
0 Votes"
LesHay-2099 answered LesHay-2099 commented

Hi

The problem you are seeing is that you have set up your code to read files based on the original list count and then deleted one or more files causing the possibility og trying to access the list with a non existant item index.
The crux of the issue is that modifying a list while it being used will likely cause this problem.
Here is a stand alone example that may help.

 Option Strict On
 Option Explicit On
 ' Form1 with a KistBox1,
 ' Button named 'BtnBibleVerseMsgBox2Okay',
 ' Label named 'LblBibleVerses3'
 ' and Timer2
    
 Public Class Form1
     ' changed to a more useful container
     Dim files As New List(Of String)
     ' added a binding source
     Dim BS As New BindingSource
    
     Dim Pause As Integer = 3
     Dim pointer As Integer = 0
     ' made a test full path string
     Dim folder As String = "C:\Users\lesha\Desktop\New folder"
    
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         files = IO.Directory.GetFiles(folder, "*.txt").ToList
         BS.DataSource = files
         ListBox1.DataSource = BS
         ListBox1.SelectedIndex = -1
         Timer2.Enabled = True
     End Sub
    
     Private Sub BtnBibleVerseMsgBox2Okay_Click(sender As Object, e As EventArgs) Handles BtnBibleVerseMsgBox2Okay.Click
         Dim si As Integer = ListBox1.SelectedIndex
         ' ensure there is an item selected
         If si > -1 Then
             Dim fn As String = ListBox1.SelectedItem.ToString
    
             ' ensure that such a file exists
             ' before trying to delete
             If IO.File.Exists(fn) Then IO.File.Delete(fn)
    
             BS.RemoveAt(si)
         End If
     End Sub
     Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
         Timer2.Interval = Pause * 1000
         Try
             LblBibleVerses3.Text = IO.File.ReadAllText(files(pointer))
         Catch ex As Exception
             ' list was modified (item deleted) and
             ' current pointer exceeds new item count
             ' so, reset pointer to 0
             pointer = 0
         End Try
         pointer += 1
         If pointer > files.Count - 1 Then pointer = 0
     End Sub
 End Class
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much, LesHay-2099. However, i got a conflicting error in ListBox1_SelectedIndexChanged event. See attached for error message.

[1]: /answers/storage/attachments/197532-capture-20220428-230509.png

0 Votes 0 ·
LesHay-2099 avatar image LesHay-2099 WilfredEghenedji-9495 ·

Hi
The error message indicates exactly what is causing the issue. You have a list of strings and then trying to cast one of them (a string) to a IO.FileInfo and that won't work. The ListBox items are strings.
The code I posted - reasonably tested for exceptions - works fine - did you try it as a stand alone example?

1 Vote 1 ·

Yes, as a standalone example, it works fine, but using it in my project, it brings up the error. What could I possibly alter in the ListBox1_SelectedIndexChanged event, as shown in the attached image?

0 Votes 0 ·
JiachenLiMFST-9349 avatar image
0 Votes"
JiachenLiMFST-9349 answered JiachenLiMFST-9349 edited

Hi @WilfredEghenedji-9495 ,
I used your code to test and it didn't produce the same error.

  • Please confirm whether the file address is accurate and whether there is a corresponding file in the bin\debug\ph1 directory of the project.

  • The files list should be updated after deleting files.

Best Regards.
Jiachen Li


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Jiachen Li, thank you for your response. After deleting an item in the listbox, did you wait until 5 seconds before closing your application? The error comes-up after 5 seconds - see:

 Dim Pause As Integer = 5
 Timer2.Interval = Pause * 1000
0 Votes 0 ·
JiachenLiMFST-9349 avatar image JiachenLiMFST-9349 WilfredEghenedji-9495 ·

Below is the code I tested and it did not produce an error.

     Dim files() As String
     Dim Pause As Integer = 5
     Dim pointer As Integer = 0
     Dim folder As String = "txt\"
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         If ListBox1.SelectedItem Is Nothing Then
             Return
         End If
         If ListBox1.SelectedIndex <> -1 Then
             System.IO.File.Delete(ListBox1.Items(ListBox1.SelectedIndex).fullname())
             ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
             ListBox1.SelectedIndex = 0
         End If
     End Sub
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         files = IO.Directory.GetFiles(folder, "*.txt")
         Timer2.Enabled = True
         For Each Str As String In files
             ListBox1.Items.Add(New FileInfo(Str))
         Next
     End Sub
     Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
         If ListBox1.SelectedItem IsNot Nothing Then
             Dim fname As String = (CType(ListBox1.SelectedItem, FileInfo)).FullName
             Console.WriteLine(fname)
         End If
     End Sub
1 Vote 1 ·
JiachenLiMFST-9349 avatar image JiachenLiMFST-9349 WilfredEghenedji-9495 ·
     Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
         Timer2.Interval = Pause * 1000
         LblBibleVerses3.Text = IO.File.ReadAllText(files(Pointer))
         Pointer += 1
         If Pointer > files.Count - 1 Then Pointer = 0
     End Sub
1 Vote 1 ·