Removing multiple items from a multi-select ListView

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phill W.

    #1

    Removing multiple items from a multi-select ListView

    (VB'2003)
    What's the correct way to remove multiple, selected items from a
    ListView control (say, from a ContextMenu)?

    I ask because I'm getting a very annoying ArgumentOutOfRa ngeException
    because the ListView seems to be trying to "re-select" items that are no
    longer there!

    for example, giventhat I have 3 items in my list:
    Select the first and remove it - no problem.
    Select the last /but one/ and remove it - still no problem.
    Select the /last/ item and remove it - Boom!

    ArgumentOutOfRa ngeException
    Parameter name: '2' is not a valid value for 'displayIndex'
    at S.W.F.ListViewI temCollection.g etItem(Int32 displayIndex)
    at S.W.F.ListView. LvnBeginDrag(Mo useButtons buttons, NMLISTVIEW mnlv)
    ...

    It's as if the "list" of select indices is being re-applied, even though
    the items to which those items correspond[ed] have been removed.

    Any suggestions?

    [Code]
    Dim iIndices As Integer()
    ReDim iIndices(Me.lvM embers.Selected Indices.Count - 1)
    Me.lvMembers.Se lectedIndices.C opyTo(iIndices, 0)

    For iIndex As Integer = iIndices.Length - 1 To 0 Step -1
    Dim oItem As ListViewItem _
    = Me.lvMembers.It ems(iIndices(iI ndex))

    oItem.Selected = False ' even tried this!
    Me.lvMembers.It ems.Remove(oIte m)
    Next

    TIA,
    Phill W.
  • Samuel Shulman

    #2
    Re: Removing multiple items from a multi-select ListView

    Why do you select the item to remove it
    Get the index of the item you want to remove and use the
    ListView.Items. RemoveAt method

    hth,
    Samuel

    "Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
    news:eaadhl$4ga $1@south.jnrs.j a.net...
    (VB'2003)
    What's the correct way to remove multiple, selected items from a ListView
    control (say, from a ContextMenu)?
    >
    I ask because I'm getting a very annoying ArgumentOutOfRa ngeException
    because the ListView seems to be trying to "re-select" items that are no
    longer there!
    >
    for example, giventhat I have 3 items in my list:
    Select the first and remove it - no problem.
    Select the last /but one/ and remove it - still no problem.
    Select the /last/ item and remove it - Boom!
    >
    ArgumentOutOfRa ngeException
    Parameter name: '2' is not a valid value for 'displayIndex'
    at S.W.F.ListViewI temCollection.g etItem(Int32 displayIndex)
    at S.W.F.ListView. LvnBeginDrag(Mo useButtons buttons, NMLISTVIEW mnlv)
    ...
    >
    It's as if the "list" of select indices is being re-applied, even though
    the items to which those items correspond[ed] have been removed.
    >
    Any suggestions?
    >
    [Code]
    Dim iIndices As Integer()
    ReDim iIndices(Me.lvM embers.Selected Indices.Count - 1)
    Me.lvMembers.Se lectedIndices.C opyTo(iIndices, 0)
    >
    For iIndex As Integer = iIndices.Length - 1 To 0 Step -1
    Dim oItem As ListViewItem _
    = Me.lvMembers.It ems(iIndices(iI ndex))
    >
    oItem.Selected = False ' even tried this!
    Me.lvMembers.It ems.Remove(oIte m)
    Next
    >
    TIA,
    Phill W.

    Comment

    • Phill W.

      #3
      Re: Removing multiple items from a multi-select ListView

      Samuel,
      Why do you select the item to remove it
      I don't - I'm trying to "un-select" it in the vain hope that the
      ListView control won't then try to redisplay it in a "selected" way (or
      /any/ way, come to that), which is what's causing it to blow up.
      Get the index of the item you want to remove and use the
      ListView.Items. RemoveAt method
      Yep - tried that. Same results using Remove(item) and RemoveAt(index) .

      Regards,
      Phill W.

      Samuel Shulman wrote:
      Why do you select the item to remove it
      Get the index of the item you want to remove and use the
      ListView.Items. RemoveAt method
      >
      hth,
      Samuel
      >
      "Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
      news:eaadhl$4ga $1@south.jnrs.j a.net...
      >(VB'2003)
      >What's the correct way to remove multiple, selected items from a ListView
      >control (say, from a ContextMenu)?
      >>
      >I ask because I'm getting a very annoying ArgumentOutOfRa ngeException
      >because the ListView seems to be trying to "re-select" items that are no
      >longer there!
      >>
      >for example, giventhat I have 3 items in my list:
      >Select the first and remove it - no problem.
      >Select the last /but one/ and remove it - still no problem.
      >Select the /last/ item and remove it - Boom!
      >>
      >ArgumentOutOfR angeException
      >Parameter name: '2' is not a valid value for 'displayIndex'
      > at S.W.F.ListViewI temCollection.g etItem(Int32 displayIndex)
      > at S.W.F.ListView. LvnBeginDrag(Mo useButtons buttons, NMLISTVIEW mnlv)
      > ...
      >>
      >It's as if the "list" of select indices is being re-applied, even though
      >the items to which those items correspond[ed] have been removed.
      >>
      >Any suggestions?
      >>
      >[Code]
      >Dim iIndices As Integer()
      >ReDim iIndices(Me.lvM embers.Selected Indices.Count - 1)
      >Me.lvMembers.S electedIndices. CopyTo(iIndices , 0)
      >>
      >For iIndex As Integer = iIndices.Length - 1 To 0 Step -1
      > Dim oItem As ListViewItem _
      > = Me.lvMembers.It ems(iIndices(iI ndex))
      >>
      > oItem.Selected = False ' even tried this!
      > Me.lvMembers.It ems.Remove(oIte m)
      >Next
      >>
      >TIA,
      > Phill W.
      >
      >

      Comment

      • Phill W.

        #4
        Re: Removing multiple items from a multi-select ListView

        Phill W. wrote:
        (VB'2003)
        What's the correct way to remove multiple, selected items from a
        ListView control (say, from a ContextMenu)?
        >
        I ask because I'm getting a very annoying ArgumentOutOfRa ngeException
        because the ListView seems to be trying to "re-select" items that are no
        longer there!
        >
        for example, giventhat I have 3 items in my list:
        Select the first and remove it - no problem.
        Select the last /but one/ and remove it - still no problem.
        Select the /last/ item and remove it - Boom!
        >
        ArgumentOutOfRa ngeException
        Parameter name: '2' is not a valid value for 'displayIndex'
        at S.W.F.ListViewI temCollection.g etItem(Int32 displayIndex)
        at S.W.F.ListView. LvnBeginDrag(Mo useButtons buttons, NMLISTVIEW mnlv)
        ...
        >
        It's as if the "list" of select indices is being re-applied, even though
        the items to which those items correspond[ed] have been removed.
        >
        Any suggestions?
        >
        [Code]
        Dim iIndices As Integer()
        ReDim iIndices(Me.lvM embers.Selected Indices.Count - 1)
        Me.lvMembers.Se lectedIndices.C opyTo(iIndices, 0)
        >
        For iIndex As Integer = iIndices.Length - 1 To 0 Step -1
        Dim oItem As ListViewItem _
        = Me.lvMembers.It ems(iIndices(iI ndex))
        >
        oItem.Selected = False ' even tried this!
        Me.lvMembers.It ems.Remove(oIte m)
        Next
        >
        TIA,
        Phill W.
        At the risk of seeming to talk to myself (not a first, by any means) it
        looks like the SelectedIndices might be a Red Herring.

        I /only/ get this problem if the item corresponding to the last item I
        selected (by Clicking and Ctrl-Clicking in the ListView) is removed, so...
        If I select the last item in the list, then the first, I can remove them
        both and the first item remains selected.
        If I select the first item, then the last, and remove them - Boom!

        Regards,
        Phill W.

        Comment

        • Samuel Shulman

          #5
          Re: Removing multiple items from a multi-select ListView

          Maybe that,
          When you actually remove an item then the index of the other items changes
          and if say the maximum index value was 10 after removing any item it will be
          9

          You can do 1 of the following:
          1. Record to some variables (array) a unique value of the items you want to
          remove and find out the index of that item each time based on that value
          then usse the removeAt

          2. Reference all the items to remove to a created array of items then loop
          through this array (I hope that the valuue of the index will change as you
          go along)


          hth,
          Samuel Shulman



          "Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
          news:eaalaa$6pr $2@south.jnrs.j a.net...
          Samuel,
          >
          Why do you select the item to remove it
          I don't - I'm trying to "un-select" it in the vain hope that the ListView
          control won't then try to redisplay it in a "selected" way (or /any/ way,
          come to that), which is what's causing it to blow up.
          >
          Get the index of the item you want to remove and use the
          ListView.Items. RemoveAt method
          >
          Yep - tried that. Same results using Remove(item) and RemoveAt(index) .
          >
          Regards,
          Phill W.
          >
          Samuel Shulman wrote:
          >Why do you select the item to remove it
          >Get the index of the item you want to remove and use the
          >ListView.Items .RemoveAt method
          >>
          >hth,
          >Samuel
          >>
          >"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
          >news:eaadhl$4g a$1@south.jnrs. ja.net...
          >>(VB'2003)
          >>What's the correct way to remove multiple, selected items from a
          >>ListView control (say, from a ContextMenu)?
          >>>
          >>I ask because I'm getting a very annoying ArgumentOutOfRa ngeException
          >>because the ListView seems to be trying to "re-select" items that are no
          >>longer there!
          >>>
          >>for example, giventhat I have 3 items in my list:
          >>Select the first and remove it - no problem.
          >>Select the last /but one/ and remove it - still no problem.
          >>Select the /last/ item and remove it - Boom!
          >>>
          >>ArgumentOutOf RangeException
          >>Parameter name: '2' is not a valid value for 'displayIndex'
          >> at S.W.F.ListViewI temCollection.g etItem(Int32 displayIndex)
          >> at S.W.F.ListView. LvnBeginDrag(Mo useButtons buttons, NMLISTVIEW mnlv)
          >> ...
          >>>
          >>It's as if the "list" of select indices is being re-applied, even though
          >>the items to which those items correspond[ed] have been removed.
          >>>
          >>Any suggestions?
          >>>
          >>[Code]
          >>Dim iIndices As Integer()
          >>ReDim iIndices(Me.lvM embers.Selected Indices.Count - 1)
          >>Me.lvMembers. SelectedIndices .CopyTo(iIndice s, 0)
          >>>
          >>For iIndex As Integer = iIndices.Length - 1 To 0 Step -1
          >> Dim oItem As ListViewItem _
          >> = Me.lvMembers.It ems(iIndices(iI ndex))
          >>>
          >> oItem.Selected = False ' even tried this!
          >> Me.lvMembers.It ems.Remove(oIte m)
          >>Next
          >>>
          >>TIA,
          >> Phill W.
          >>

          Comment

          • Phill W.

            #6
            Re: Removing multiple items from a multi-select ListView

            Samuel,

            Found it!

            Old habits die hard: I was displaying the Context Menu manually (using
            ContextMenu.Sho w) but from the ListView's Mouse/Down/ event instead of
            Mouse/Up/.

            Moved my code into[ListView].MouseUp and everything works as expected!

            Regards,
            Phill W.

            Samuel Shulman wrote:
            Maybe that,
            When you actually remove an item then the index of the other items changes
            and if say the maximum index value was 10 after removing any item it will be
            9
            >
            You can do 1 of the following:
            1. Record to some variables (array) a unique value of the items you want to
            remove and find out the index of that item each time based on that value
            then usse the removeAt
            >
            2. Reference all the items to remove to a created array of items then loop
            through this array (I hope that the valuue of the index will change as you
            go along)
            >
            >
            hth,
            Samuel Shulman
            >
            >
            >
            "Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
            news:eaalaa$6pr $2@south.jnrs.j a.net...
            >Samuel,
            >>
            >>Why do you select the item to remove it
            >I don't - I'm trying to "un-select" it in the vain hope that the ListView
            >control won't then try to redisplay it in a "selected" way (or /any/ way,
            >come to that), which is what's causing it to blow up.
            >>
            >>Get the index of the item you want to remove and use the
            >>ListView.Item s.RemoveAt method
            >Yep - tried that. Same results using Remove(item) and RemoveAt(index) .
            >>
            >Regards,
            > Phill W.
            >>
            >Samuel Shulman wrote:
            >>Why do you select the item to remove it
            >>Get the index of the item you want to remove and use the
            >>ListView.Item s.RemoveAt method
            >>>
            >>hth,
            >>Samuel
            >>>
            >>"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
            >>news:eaadhl$4 ga$1@south.jnrs .ja.net...
            >>>(VB'2003)
            >>>What's the correct way to remove multiple, selected items from a
            >>>ListView control (say, from a ContextMenu)?
            >>>>
            >>>I ask because I'm getting a very annoying ArgumentOutOfRa ngeException
            >>>because the ListView seems to be trying to "re-select" items that are no
            >>>longer there!
            >>>>
            >>>for example, giventhat I have 3 items in my list:
            >>>Select the first and remove it - no problem.
            >>>Select the last /but one/ and remove it - still no problem.
            >>>Select the /last/ item and remove it - Boom!
            >>>>
            >>>ArgumentOutO fRangeException
            >>>Parameter name: '2' is not a valid value for 'displayIndex'
            >>> at S.W.F.ListViewI temCollection.g etItem(Int32 displayIndex)
            >>> at S.W.F.ListView. LvnBeginDrag(Mo useButtons buttons, NMLISTVIEW mnlv)
            >>> ...
            >>>>
            >>>It's as if the "list" of select indices is being re-applied, even though
            >>>the items to which those items correspond[ed] have been removed.
            >>>>
            >>>Any suggestions?
            >>>>
            >>>[Code]
            >>>Dim iIndices As Integer()
            >>>ReDim iIndices(Me.lvM embers.Selected Indices.Count - 1)
            >>>Me.lvMembers .SelectedIndice s.CopyTo(iIndic es, 0)
            >>>>
            >>>For iIndex As Integer = iIndices.Length - 1 To 0 Step -1
            >>> Dim oItem As ListViewItem _
            >>> = Me.lvMembers.It ems(iIndices(iI ndex))
            >>>>
            >>> oItem.Selected = False ' even tried this!
            >>> Me.lvMembers.It ems.Remove(oIte m)
            >>>Next
            >>>>
            >>>TIA,
            >>> Phill W.
            >

            Comment

            Working...