简体   繁体   中英

Error when removing selected items from a listbox

Scenario

I have 2 listboxes 1 for an order and another for the total cost. To make it user to use i used the following code to link up the selected items.

Dim Selecting As Boolean = False
Private Sub lstOrders_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstOrders.SelectedIndexChanged
    If Not Selecting Then
        Selecting = True
        lstTotalsEachOrder.SetSelected(lstOrders.SelectedIndex, True)
        Selecting = False
    End If
End Sub
Private Sub lstTotalsEachOrder_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstTotalsEachOrder.SelectedIndexChanged
    If Not Selecting Then
        Selecting = True
        lstOrders.SetSelected(lstTotalsEachOrder.SelectedIndex, True)
        Selecting = False
    End If
End Sub

The problem comes in when i attempt to remove an item use the following code.

        lstOrders.Items.RemoveAt(lstOrders.Items.Count - 1)
        lstTotalsEachOrder.Items.RemoveAt(lstTotalsEachOrder.Items.Count - 1)

The Problem

It works fine removing items, but when it reaches a selected item it throws the error: InvalidArgument=Value of '-1' is not valid for 'index'.

I attempted to fix this using the following code

lstOrders.ClearSelected()
lstTotalsEachOrder.ClearSelected()

But i still get the error.

Any help would be appreciated.

Are you sure that the problem is with the selected items? Seems to me that you are getting this exception after you try to remove items from an empty ListBox .

Use a simple condition to make sure the ListBox actually have items inside:

If lstOrders.Items.Count > 0 Then 
    lstOrders.Items.RemoveAt(lstOrders.Items.Count - 1)
End If
If lstTotalsEachOrder.Items.Count > 0 Then 
    lstTotalsEachOrder.Items.RemoveAt(lstTotalsEachOrder.Items.Count - 1)
End If

Of course, you can wrap it all in a sub like this:

Sub RemoveLastItem(byVal lst as ListBox)
    If lst.Items.Count > 0 Then 
        lst.Items.RemoveAt(lst.Items.Count - 1)
    End if
End Sub

and then simply call this sub from your code:

RemoveLastItem(lstOrders)
RemoveLastItem(lstTotalsEachOrder)

As your Error message says:

InvalidArgument=Value of '-1' is not valid for 'index'.

Which indicates that you are trying to remove the ListItem @ -1th index. which is not present in the collection. I hope that this will happens only when you are trying to delete some items when the list box is empty(ie., lstOrders.Items.Count=0 )

hence lstOrders.Items.RemoveAt(lstOrders.Items.Count - 1) it result in removing the item @ -1th index.

And also you have to notice that your code will always delete the last item from the Listbox.

My suggestion: You can avoid this by using the above suggestion, or in simple you can use [SelectedIndex][1] property of a ListBox to find and deleted the selected value.

I found that the problem occured because there was sum sort of mismatch in the code. The error was thrown in the following code

 Dim Selecting As Boolean = False
Private Sub lstOrders_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstOrders.SelectedIndexChanged
    If Not Selecting Then
        Selecting = True
        lstTotalsEachOrder.SetSelected(lstOrders.SelectedIndex, True)
        Selecting = False
    End If
End Sub
Private Sub lstTotalsEachOrder_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstTotalsEachOrder.SelectedIndexChanged
    If Not Selecting Then
        Selecting = True
        lstOrders.SetSelected(lstTotalsEachOrder.SelectedIndex, True)
        Selecting = False
    End If
End Sub

So i though if i attempted to deselect the times then remove the item it would be fine so i wrote this before removing the items

lstOrders.ClearSelected()
lstTotalsEachOrder.ClearSelected()

But still the error occured, so i attached the following code to a button and this was also triggering the error. So i fixed this by doing the following,

    Selecting = True

    lstOrders.ClearSelected()
    lstTotalsEachOrder.ClearSelected()

    Selecting = False

This allowed me to deselect the items and remove them from the listbox's.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM