Delete Checked Item from CheckedListView

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

    #1

    Delete Checked Item from CheckedListView

    I want to iterate through a CheckedListView and delete the checked items ONLY.
    Anybody know how to do this? Anybody know a rock solid way of doing this?
    I have tried the following but get an exception

    unhandled exception of type System.InvalidO perationExcepti on in
    system.windows. forms.dll

    Additionlal Info: The list that this enumerator is bound to has been
    modified.
    An enumerator can only be used if the list dosn't change.

    Private Sub btnDelete_Click (ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles btnDelete.Click
    Dim indexChecked, itemCount As Integer
    Dim itemChecked As Object = ChkListBoxRouti nes.CheckedItem s

    For Each itemChecked In itemChecked
    ChkListBoxRouti nes.Items.Remov eAt(indexChecke d)
    ChkListBoxRouti nes.Refresh()
    Next
    End Sub







  • Robin Tucker

    #2
    Re: Delete Checked Item from CheckedListView

    Wht is "itemChecke d"? I usually fill a collection with items to remove
    first, in order that I don't modify the collection while I'm iterating it.


    Dim toRemove as New ArrayList

    For Each theItem As <someclass> In <somecollection toremovefrom>
    toRemove.Add ( theItem )
    Next



    For Each toRemove As <someclass> In toRemove
    <somecollection toremovefrom>.R emove ( toRemove )
    Next


    (also please use option strict on - it makes your code a lot more
    robust....)





    "marcmc" <marcmc@discuss ions.microsoft. com> wrote in message
    news:5F3D25CA-F373-429B-A64B-E966ACC1B8E0@mi crosoft.com...[color=blue]
    >I want to iterate through a CheckedListView and delete the checked items
    >ONLY.
    > Anybody know how to do this? Anybody know a rock solid way of doing this?
    > I have tried the following but get an exception
    >
    > unhandled exception of type System.InvalidO perationExcepti on in
    > system.windows. forms.dll
    >
    > Additionlal Info: The list that this enumerator is bound to has been
    > modified.
    > An enumerator can only be used if the list dosn't change.
    >
    > Private Sub btnDelete_Click (ByVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles btnDelete.Click
    > Dim indexChecked, itemCount As Integer
    > Dim itemChecked As Object = ChkListBoxRouti nes.CheckedItem s
    >
    > For Each itemChecked In itemChecked
    > ChkListBoxRouti nes.Items.Remov eAt(indexChecke d)
    > ChkListBoxRouti nes.Refresh()
    > Next
    > End Sub
    >
    >
    >
    >
    >
    >
    >[/color]


    Comment

    • marcmc

      #3
      Re: Delete Checked Item from CheckedListView

      Sorry Robin,
      I don't understand your code too well
      It's the <someclass> & <somecollection toremovefrom> I need help with.
      Can you post a working example if you have time, I'ld really appreciate it.
      marc


      "Robin Tucker" wrote:
      [color=blue]
      > Wht is "itemChecke d"? I usually fill a collection with items to remove
      > first, in order that I don't modify the collection while I'm iterating it.
      >
      >
      > Dim toRemove as New ArrayList
      >
      > For Each theItem As <someclass> In <somecollection toremovefrom>
      > toRemove.Add ( theItem )
      > Next
      >
      >
      >
      > For Each toRemove As <someclass> In toRemove
      > <somecollection toremovefrom>.R emove ( toRemove )
      > Next
      >
      >
      > (also please use option strict on - it makes your code a lot more
      > robust....)
      >
      >
      >
      >
      >
      > "marcmc" <marcmc@discuss ions.microsoft. com> wrote in message
      > news:5F3D25CA-F373-429B-A64B-E966ACC1B8E0@mi crosoft.com...[color=green]
      > >I want to iterate through a CheckedListView and delete the checked items
      > >ONLY.
      > > Anybody know how to do this? Anybody know a rock solid way of doing this?
      > > I have tried the following but get an exception
      > >
      > > unhandled exception of type System.InvalidO perationExcepti on in
      > > system.windows. forms.dll
      > >
      > > Additionlal Info: The list that this enumerator is bound to has been
      > > modified.
      > > An enumerator can only be used if the list dosn't change.
      > >
      > > Private Sub btnDelete_Click (ByVal sender As System.Object, ByVal e As
      > > System.EventArg s) Handles btnDelete.Click
      > > Dim indexChecked, itemCount As Integer
      > > Dim itemChecked As Object = ChkListBoxRouti nes.CheckedItem s
      > >
      > > For Each itemChecked In itemChecked
      > > ChkListBoxRouti nes.Items.Remov eAt(indexChecke d)
      > > ChkListBoxRouti nes.Refresh()
      > > Next
      > > End Sub
      > >
      > >
      > >
      > >
      > >
      > >
      > >[/color]
      >
      >
      >[/color]

      Comment

      • Robin Tucker

        #4
        Re: Delete Checked Item from CheckedListView

        Something like this:


        Private Sub deleteButton_Cl ick(ByVal sender As System.Object, ByVal e As
        System.EventArg s) Handles deleteButton.Cl ick

        Dim toRemove As New ArrayList

        For Each theItem As Object In CheckedListBox1 .CheckedItems
        toRemove.Add(th eItem)
        Next

        For Each theItem As Object In toRemove
        CheckedListBox1 .Items.Remove(t heItem)
        Next

        End Sub


        "marcmc" <marcmc@discuss ions.microsoft. com> wrote in message
        news:67F17F19-31F4-460C-AD02-F21C165BB89C@mi crosoft.com...[color=blue]
        > Sorry Robin,
        > I don't understand your code too well
        > It's the <someclass> & <somecollection toremovefrom> I need help with.
        > Can you post a working example if you have time, I'ld really appreciate
        > it.
        > marc
        >
        >
        > "Robin Tucker" wrote:
        >[color=green]
        >> Wht is "itemChecke d"? I usually fill a collection with items to remove
        >> first, in order that I don't modify the collection while I'm iterating
        >> it.
        >>
        >>
        >> Dim toRemove as New ArrayList
        >>
        >> For Each theItem As <someclass> In <somecollection toremovefrom>
        >> toRemove.Add ( theItem )
        >> Next
        >>
        >>
        >>
        >> For Each toRemove As <someclass> In toRemove
        >> <somecollection toremovefrom>.R emove ( toRemove )
        >> Next
        >>
        >>
        >> (also please use option strict on - it makes your code a lot more
        >> robust....)
        >>
        >>
        >>
        >>
        >>
        >> "marcmc" <marcmc@discuss ions.microsoft. com> wrote in message
        >> news:5F3D25CA-F373-429B-A64B-E966ACC1B8E0@mi crosoft.com...[color=darkred]
        >> >I want to iterate through a CheckedListView and delete the checked items
        >> >ONLY.
        >> > Anybody know how to do this? Anybody know a rock solid way of doing
        >> > this?
        >> > I have tried the following but get an exception
        >> >
        >> > unhandled exception of type System.InvalidO perationExcepti on in
        >> > system.windows. forms.dll
        >> >
        >> > Additionlal Info: The list that this enumerator is bound to has been
        >> > modified.
        >> > An enumerator can only be used if the list dosn't change.
        >> >
        >> > Private Sub btnDelete_Click (ByVal sender As System.Object, ByVal e As
        >> > System.EventArg s) Handles btnDelete.Click
        >> > Dim indexChecked, itemCount As Integer
        >> > Dim itemChecked As Object = ChkListBoxRouti nes.CheckedItem s
        >> >
        >> > For Each itemChecked In itemChecked
        >> > ChkListBoxRouti nes.Items.Remov eAt(indexChecke d)
        >> > ChkListBoxRouti nes.Refresh()
        >> > Next
        >> > End Sub
        >> >
        >> >
        >> >
        >> >
        >> >
        >> >
        >> >[/color]
        >>
        >>
        >>[/color][/color]


        Comment

        • marcmc

          #5
          Re: Delete Checked Item from CheckedListView

          thanks Robin Ill try it
          marc

          "Robin Tucker" wrote:
          [color=blue]
          > Something like this:
          >
          >
          > Private Sub deleteButton_Cl ick(ByVal sender As System.Object, ByVal e As
          > System.EventArg s) Handles deleteButton.Cl ick
          >
          > Dim toRemove As New ArrayList
          >
          > For Each theItem As Object In CheckedListBox1 .CheckedItems
          > toRemove.Add(th eItem)
          > Next
          >
          > For Each theItem As Object In toRemove
          > CheckedListBox1 .Items.Remove(t heItem)
          > Next
          >
          > End Sub
          >
          >
          > "marcmc" <marcmc@discuss ions.microsoft. com> wrote in message
          > news:67F17F19-31F4-460C-AD02-F21C165BB89C@mi crosoft.com...[color=green]
          > > Sorry Robin,
          > > I don't understand your code too well
          > > It's the <someclass> & <somecollection toremovefrom> I need help with.
          > > Can you post a working example if you have time, I'ld really appreciate
          > > it.
          > > marc
          > >
          > >
          > > "Robin Tucker" wrote:
          > >[color=darkred]
          > >> Wht is "itemChecke d"? I usually fill a collection with items to remove
          > >> first, in order that I don't modify the collection while I'm iterating
          > >> it.
          > >>
          > >>
          > >> Dim toRemove as New ArrayList
          > >>
          > >> For Each theItem As <someclass> In <somecollection toremovefrom>
          > >> toRemove.Add ( theItem )
          > >> Next
          > >>
          > >>
          > >>
          > >> For Each toRemove As <someclass> In toRemove
          > >> <somecollection toremovefrom>.R emove ( toRemove )
          > >> Next
          > >>
          > >>
          > >> (also please use option strict on - it makes your code a lot more
          > >> robust....)
          > >>
          > >>
          > >>
          > >>
          > >>
          > >> "marcmc" <marcmc@discuss ions.microsoft. com> wrote in message
          > >> news:5F3D25CA-F373-429B-A64B-E966ACC1B8E0@mi crosoft.com...
          > >> >I want to iterate through a CheckedListView and delete the checked items
          > >> >ONLY.
          > >> > Anybody know how to do this? Anybody know a rock solid way of doing
          > >> > this?
          > >> > I have tried the following but get an exception
          > >> >
          > >> > unhandled exception of type System.InvalidO perationExcepti on in
          > >> > system.windows. forms.dll
          > >> >
          > >> > Additionlal Info: The list that this enumerator is bound to has been
          > >> > modified.
          > >> > An enumerator can only be used if the list dosn't change.
          > >> >
          > >> > Private Sub btnDelete_Click (ByVal sender As System.Object, ByVal e As
          > >> > System.EventArg s) Handles btnDelete.Click
          > >> > Dim indexChecked, itemCount As Integer
          > >> > Dim itemChecked As Object = ChkListBoxRouti nes.CheckedItem s
          > >> >
          > >> > For Each itemChecked In itemChecked
          > >> > ChkListBoxRouti nes.Items.Remov eAt(indexChecke d)
          > >> > ChkListBoxRouti nes.Refresh()
          > >> > Next
          > >> > End Sub
          > >> >
          > >> >
          > >> >
          > >> >
          > >> >
          > >> >
          > >> >
          > >>
          > >>
          > >>[/color][/color]
          >
          >
          >[/color]

          Comment

          • alantolan@users.com

            #6
            Re: Delete Checked Item from CheckedListView

            >Private Sub btnDelete_Click (ByVal sender As System.Object, ByVal e As[color=blue]
            >System.EventAr gs) Handles btnDelete.Click
            > Dim indexChecked, itemCount As Integer
            > Dim itemChecked As Object = ChkListBoxRouti nes.CheckedItem ­s[/color]
            [color=blue]
            > For Each itemChecked In itemChecked
            > ChkListBoxRouti nes.Items.Remov ­eAt(indexCheck ed)
            > ChkListBoxRouti nes.Refresh()
            > Next
            > End Sub[/color]


            Try using Remove() instead of RemoveAt(). The following worked for me

            For Each itm As ListViewItem In lvwMain.Checked Items
            lvwMain.Items.R emove(itm)
            Next

            There was no need for a refresh - repainted itself after removing the
            items.

            Comment

            Working...