none
Remove a selected item from selected index.

    Question

  • Hi,  I have a listbox that contains a list of code numbers, when I go through the listbox and select multiple items the program builds a unique code number from the selections.

    The issue is that if the user unselects an item I want that item removed from the selected index.

    How it currently works, if I select "F" it is hilighted, if I select it again it is no longer hilighted though it remains in the selectedindex.  When the application runs it still picks up "F" even though it is not shown as selected in the listbox.

    I can go in and remove the selected item "F" so that it no longer appears in the listbox.  While the item "F" is removed from the listbox, the index is still set to the postion of the "F".

    So now the application picks up the item that is now in that index space, so instead of "F", I am now getting "G".

    My code to remove the item from the listbox

    listbox.Items.Remove(listbox.SelectedItems(i))

    I want to have the item removed from the selected index when it is unselected, though still available in the listbox to be selected again.

    Thanks for your help.  Dale

    Thursday, July 26, 2012 3:05 PM

Answers

  • OK, well I'm still not sure exactly what you are doing but keep in mind that when you remove an item in a collection it will impact the order (or index) of other items in the collection that follow it. Perhaps you want to have the loop operate in reverse direction, processing the last items first, which would not impact preceding items in the collection.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, July 26, 2012 5:19 PM
  • Dale,

    See if this might help.

    I have the code on a page of my web site here and following are the results. I first select some then click the button to see the results, then de-select one and do that again:


    Please call me Frank :)

    Thursday, July 26, 2012 4:44 PM

All replies

  • An item in a ListBox should not show up in the SelectedItems if it is not selected. You may need to post code to illustrate your issue.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, July 26, 2012 3:16 PM
  • Hi, the code is pretty basic, just go through the selecteditems and call a routine that takes the selected item and builds a code.  So I am just wanting the unselected item to be removed from the selected index.

    With the item is unselected it still gets picked up by the following code as it still exists in the selected index of the listbox.

    For i= 0 To listbox.SelectedItems.Count  - 1

          Dim CodeCheck as String = listbox.SelectedItems(i).ToString()

          Call BuildCode (CodeCheck)

    Next i

    I have tried this:

    Dim index as Integer = listbox.SelectedIndex

    While index <> -1

         For i= 0 To listbox.SelectedItems.Count  - 1

               Dim CodeCheck as String = listbox.SelectedItems(i).ToString()

               If theSelected then  (this is being set by a mouseup routine that detemines the selection state of the item in the lsitbox)

                       Call BuildCode (CodeCheck)

               Else

                      listbox.Items.Remove(listbox.SelectedItems(i))  (This is where I am trying to clear the selected item from the listbox/index.  This removes the item visually from the listbox, but the index of the item is still selected so the item that is in it's spot is now selected.

                end if

         Next i

    End While


    • Edited by Dale Jay Thursday, July 26, 2012 3:43 PM Submitted before it was ready.
    Thursday, July 26, 2012 3:35 PM
  • Dale,

    See if this might help.

    I have the code on a page of my web site here and following are the results. I first select some then click the button to see the results, then de-select one and do that again:


    Please call me Frank :)

    Thursday, July 26, 2012 4:44 PM
  • OK, well I'm still not sure exactly what you are doing but keep in mind that when you remove an item in a collection it will impact the order (or index) of other items in the collection that follow it. Perhaps you want to have the loop operate in reverse direction, processing the last items first, which would not impact preceding items in the collection.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, July 26, 2012 5:19 PM