Delete SelectedItem in Listbox from Folder and Listbox

Wilfred Eghenedji 326 Reputation points
2022-04-22T04:27:55.003+00:00

Please, I am intending to delete Listbox item i.e. delete selected item from both folder and Listbox. Implementing the following code, the file was deleted accordingly, but i got a conflicting error with a previous line of code, which terminated the application. Please, see attachment to view error. Thank you.

If ListBox1.SelectedIndex <> -1 Then  
     System.IO.File.Delete(ListBox1.Items(ListBox1.SelectedIndex).fullname())  
     ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)  
End If  
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.1K Reputation points
    2022-04-22T06:40:49.833+00:00

    Try adding the next lines to beginning of ListBox1_SelectedIndexChanged:

    if ListBox1.SelectedItem Is Nothing Then 
       Return
    End If
    

    Before Return, you can also change or clear the labels and other controls according to your needs.


0 additional answers

Sort by: Most helpful