question

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

Delete SelectedItem in Listbox from Folder and Listbox

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

[1]: /answers/storage/attachments/195346-capture-20220421-232836.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.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered WilfredEghenedji-9495 commented

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.


· 1
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! This worked just fine....

0 Votes 0 ·