Home All Groups Group Topic Archive Search About

unable to remove items from Listbox

Author
18 May 2009 9:54 PM
Co
Hi All,

I use 2 listboxes and I want to move items from 1 to 2.
The Items are filled with the Class MyList.
The problem is that the remove doesn't work so the items are added to
Listbox2 but also stay in 1.

            Dim dr As DataRow
            For Each dr In ds.Tables(0).Rows
                For Each mlist As Object In ListBox1.Items
                    If dr.Item("folder") = mlist.itemdata Then
                        ListBox2.Items.Add(New Mylist(mlist.name,
mlist.itemdata))
                        ListBox1.Items.Remove(mlist.name)  --> doesn't
work!!!!!!!!!!!!!!!
                    End If
                Next
            Next

Public Class Mylist
    Private sName As String
    ' You can also declare this as String,bitmap or almost anything.
    ' If you change this declaration you will also need to change the
Sub New
    ' to reflect any change. Also the ItemData Property will need to
be updated.
    Private iID As Integer

    ' Default empty constructor.
    Public Sub New()
        sName = ""
        ' This would need to be changed if you modified the
declaration above.
        iID = 0
    End Sub

    Public Sub New(ByVal Name As String, ByVal ID As Integer)
        sName = Name
        iID = ID
    End Sub

    Public Property Name() As String
        Get
            Return sName
        End Get
        Set(ByVal sValue As String)
            sName = sValue
        End Set
    End Property

    ' This is the property that holds the extra data.
    Public Property ItemData() As Int32
        Get
            Return iID
        End Get
        Set(ByVal iValue As Int32)
            iID = iValue
        End Set
    End Property

    ' This is neccessary because the ListBox and ComboBox rely
    ' on this method when determining the text to display.
    Public Overrides Function ToString() As String
        Return sName
    End Function

End Class

Regards
Marco
The Netherlands

Author
19 May 2009 1:57 AM
Mr. Arnold
Show quote Hide quote
"Co" <vonclausow***@gmail.com> wrote in message
news:2f859428-6dce-4a6f-b0c3-f745f79dc22b@g19g2000vbi.googlegroups.com...
> Hi All,
>
> I use 2 listboxes and I want to move items from 1 to 2.
> The Items are filled with the Class MyList.
> The problem is that the remove doesn't work so the items are added to
> Listbox2 but also stay in 1.
>
>            Dim dr As DataRow
>            For Each dr In ds.Tables(0).Rows
>                For Each mlist As Object In ListBox1.Items
>                    If dr.Item("folder") = mlist.itemdata Then
>                        ListBox2.Items.Add(New Mylist(mlist.name,
> mlist.itemdata))
>                        ListBox1.Items.Remove(mlist.name)  --> doesn't
> work!!!!!!!!!!!!!!!
>                    End If
>                Next
>            Next
>

I am surprised that you didn't get a compile error stating 'that you cannot
remove an object that's being used in a foreach loop'. You'll get that in
C#. I guess VB is forgiving by just not doing it.

You need to use a RemoveAt off of an index on a For Loop. I recall you have
to be careful doing this to prevent "index out of range message"  exception
or something like that.


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4085 (20090519) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
Author
19 May 2009 11:51 PM
OmegaSquared
Hello, Co,

I guess that the first problem that you have is that you are trying to
remove mlist.name from the ListBox, but the ListBox actually contains mlist.

When you change that, you should then see the problem that Mr. Arnold is
referring to (although I think it will bother you at run time, rather than at
compile time).  One alternate solution is to save references to the items
that need to be removed (in an ArrayList, for instance) and then when the
main processing loop is finished, loop through the ArrayList removing all the
recorded items from the ListBox.

Cheers,
Randy


Show quoteHide quote
"Co" wrote:

> Hi All,
>
> I use 2 listboxes and I want to move items from 1 to 2.
> The Items are filled with the Class MyList.
> The problem is that the remove doesn't work so the items are added to
> Listbox2 but also stay in 1.
>
>             Dim dr As DataRow
>             For Each dr In ds.Tables(0).Rows
>                 For Each mlist As Object In ListBox1.Items
>                     If dr.Item("folder") = mlist.itemdata Then
>                         ListBox2.Items.Add(New Mylist(mlist.name,
> mlist.itemdata))
>                         ListBox1.Items.Remove(mlist.name)  --> doesn't
> work!!!!!!!!!!!!!!!
>                     End If
>                 Next
>             Next
>
> Public Class Mylist
>     Private sName As String
>     ' You can also declare this as String,bitmap or almost anything.
>     ' If you change this declaration you will also need to change the
> Sub New
>     ' to reflect any change. Also the ItemData Property will need to
> be updated.
>     Private iID As Integer
>
>     ' Default empty constructor.
>     Public Sub New()
>         sName = ""
>         ' This would need to be changed if you modified the
> declaration above.
>         iID = 0
>     End Sub
>
>     Public Sub New(ByVal Name As String, ByVal ID As Integer)
>         sName = Name
>         iID = ID
>     End Sub
>
>     Public Property Name() As String
>         Get
>             Return sName
>         End Get
>         Set(ByVal sValue As String)
>             sName = sValue
>         End Set
>     End Property
>
>     ' This is the property that holds the extra data.
>     Public Property ItemData() As Int32
>         Get
>             Return iID
>         End Get
>         Set(ByVal iValue As Int32)
>             iID = iValue
>         End Set
>     End Property
>
>     ' This is neccessary because the ListBox and ComboBox rely
>     ' on this method when determining the text to display.
>     Public Overrides Function ToString() As String
>         Return sName
>     End Function
>
> End Class
>
> Regards
> Marco
> The Netherlands
>
Author
20 May 2009 8:03 AM
Cor Ligthert[MVP]
Co,

It is strange code, however, to begin here.
\\\
If dr.Item("folder") = mlist.itemdata Then  ListBox2.Items.Add(New
Mylist(mlist.name, mlist.itemdata))
///

You try to add a "new" object from the Type MyList to the Listbox2.

Why not simple that original mList object that is OOP?
Then you can remove the reference to that object from Listbox1

Cor




Show quoteHide quote
"Co" <vonclausow***@gmail.com> wrote in message
news:2f859428-6dce-4a6f-b0c3-f745f79dc22b@g19g2000vbi.googlegroups.com...
> Hi All,
>
> I use 2 listboxes and I want to move items from 1 to 2.
> The Items are filled with the Class MyList.
> The problem is that the remove doesn't work so the items are added to
> Listbox2 but also stay in 1.
>
>            Dim dr As DataRow
>            For Each dr In ds.Tables(0).Rows
>                For Each mlist As Object In ListBox1.Items
>                    If dr.Item("folder") = mlist.itemdata Then
>                        ListBox2.Items.Add(New Mylist(mlist.name,
> mlist.itemdata))
>                        ListBox1.Items.Remove(mlist.name)  --> doesn't
> work!!!!!!!!!!!!!!!
>                    End If
>                Next
>            Next
>
> Public Class Mylist
>    Private sName As String
>    ' You can also declare this as String,bitmap or almost anything.
>    ' If you change this declaration you will also need to change the
> Sub New
>    ' to reflect any change. Also the ItemData Property will need to
> be updated.
>    Private iID As Integer
>
>    ' Default empty constructor.
>    Public Sub New()
>        sName = ""
>        ' This would need to be changed if you modified the
> declaration above.
>        iID = 0
>    End Sub
>
>    Public Sub New(ByVal Name As String, ByVal ID As Integer)
>        sName = Name
>        iID = ID
>    End Sub
>
>    Public Property Name() As String
>        Get
>            Return sName
>        End Get
>        Set(ByVal sValue As String)
>            sName = sValue
>        End Set
>    End Property
>
>    ' This is the property that holds the extra data.
>    Public Property ItemData() As Int32
>        Get
>            Return iID
>        End Get
>        Set(ByVal iValue As Int32)
>            iID = iValue
>        End Set
>    End Property
>
>    ' This is neccessary because the ListBox and ComboBox rely
>    ' on this method when determining the text to display.
>    Public Overrides Function ToString() As String
>        Return sName
>    End Function
>
> End Class
>
> Regards
> Marco
> The Netherlands
Author
20 May 2009 1:50 PM
Co
Show quote Hide quote
On 20 mei, 10:03, "Cor Ligthert[MVP]" <Notmyfirstn***@planet.nl>
wrote:
> Co,
>
> It is strange code, however, to begin here.
> \\\
>  If dr.Item("folder") = mlist.itemdata Then  ListBox2.Items.Add(New
> Mylist(mlist.name, mlist.itemdata))
> ///
>
> You try to add a "new" object from the Type MyList to the Listbox2.
>
> Why not simple that original mList object that is OOP?
> Then you can remove the reference to that object from Listbox1
>
> Cor
>
> "Co" <vonclausow***@gmail.com> wrote in message
>
> news:2f859428-6dce-4a6f-b0c3-f745f79dc22b@g19g2000vbi.googlegroups.com...
>
> > Hi All,
>
> > I use 2 listboxes and I want to move items from 1 to 2.
> > The Items are filled with the Class MyList.
> > The problem is that the remove doesn't work so the items are added to
> > Listbox2 but also stay in 1.
>
> >            Dim dr As DataRow
> >            For Each dr In ds.Tables(0).Rows
> >                For Each mlist As Object In ListBox1.Items
> >                    If dr.Item("folder") = mlist.itemdata Then
> >                        ListBox2.Items.Add(New Mylist(mlist.name,
> > mlist.itemdata))
> >                        ListBox1.Items.Remove(mlist.name)  --> doesn't
> > work!!!!!!!!!!!!!!!
> >                    End If
> >                Next
> >            Next
>
> > Public Class Mylist
> >    Private sName As String
> >    ' You can also declare this as String,bitmap or almost anything.
> >    ' If you change this declaration you will also need to change the
> > Sub New
> >    ' to reflect any change. Also the ItemData Property will need to
> > be updated.
> >    Private iID As Integer
>
> >    ' Default empty constructor.
> >    Public Sub New()
> >        sName = ""
> >        ' This would need to be changed if you modified the
> > declaration above.
> >        iID = 0
> >    End Sub
>
> >    Public Sub New(ByVal Name As String, ByVal ID As Integer)
> >        sName = Name
> >        iID = ID
> >    End Sub
>
> >    Public Property Name() As String
> >        Get
> >            Return sName
> >        End Get
> >        Set(ByVal sValue As String)
> >            sName = sValue
> >        End Set
> >    End Property
>
> >    ' This is the property that holds the extra data.
> >    Public Property ItemData() As Int32
> >        Get
> >            Return iID
> >        End Get
> >        Set(ByVal iValue As Int32)
> >            iID = iValue
> >        End Set
> >    End Property
>
> >    ' This is neccessary because the ListBox and ComboBox rely
> >    ' on this method when determining the text to display.
> >    Public Overrides Function ToString() As String
> >        Return sName
> >    End Function
>
> > End Class
>
> > Regards
> > Marco
> > The Netherlands

Cor,

How would that code look like, I have no idea.

MArco
Author
21 May 2009 3:08 AM
Jack Jackson
On Wed, 20 May 2009 06:50:10 -0700 (PDT), Co <vonclausow***@gmail.com>
wrote:

Show quoteHide quote
>On 20 mei, 10:03, "Cor Ligthert[MVP]" <Notmyfirstn***@planet.nl>
>wrote:
>> Co,
>>
>> It is strange code, however, to begin here.
>> \\\
>>  If dr.Item("folder") = mlist.itemdata Then  ListBox2.Items.Add(New
>> Mylist(mlist.name, mlist.itemdata))
>> ///
>>
>> You try to add a "new" object from the Type MyList to the Listbox2.
>>
>> Why not simple that original mList object that is OOP?
>> Then you can remove the reference to that object from Listbox1
>>
>> Cor
>>
>> "Co" <vonclausow***@gmail.com> wrote in message
>>
>> news:2f859428-6dce-4a6f-b0c3-f745f79dc22b@g19g2000vbi.googlegroups.com...
>>
>> > Hi All,
>>
>> > I use 2 listboxes and I want to move items from 1 to 2.
>> > The Items are filled with the Class MyList.
>> > The problem is that the remove doesn't work so the items are added to
>> > Listbox2 but also stay in 1.
>>
>> >            Dim dr As DataRow
>> >            For Each dr In ds.Tables(0).Rows
>> >                For Each mlist As Object In ListBox1.Items
>> >                    If dr.Item("folder") = mlist.itemdata Then
>> >                        ListBox2.Items.Add(New Mylist(mlist.name,
>> > mlist.itemdata))
>> >                        ListBox1.Items.Remove(mlist.name)  --> doesn't
>> > work!!!!!!!!!!!!!!!
>> >                    End If
>> >                Next
>> >            Next
>>
>> > Public Class Mylist
>> >    Private sName As String
>> >    ' You can also declare this as String,bitmap or almost anything.
>> >    ' If you change this declaration you will also need to change the
>> > Sub New
>> >    ' to reflect any change. Also the ItemData Property will need to
>> > be updated.
>> >    Private iID As Integer
>>
>> >    ' Default empty constructor.
>> >    Public Sub New()
>> >        sName = ""
>> >        ' This would need to be changed if you modified the
>> > declaration above.
>> >        iID = 0
>> >    End Sub
>>
>> >    Public Sub New(ByVal Name As String, ByVal ID As Integer)
>> >        sName = Name
>> >        iID = ID
>> >    End Sub
>>
>> >    Public Property Name() As String
>> >        Get
>> >            Return sName
>> >        End Get
>> >        Set(ByVal sValue As String)
>> >            sName = sValue
>> >        End Set
>> >    End Property
>>
>> >    ' This is the property that holds the extra data.
>> >    Public Property ItemData() As Int32
>> >        Get
>> >            Return iID
>> >        End Get
>> >        Set(ByVal iValue As Int32)
>> >            iID = iValue
>> >        End Set
>> >    End Property
>>
>> >    ' This is neccessary because the ListBox and ComboBox rely
>> >    ' on this method when determining the text to display.
>> >    Public Overrides Function ToString() As String
>> >        Return sName
>> >    End Function
>>
>> > End Class
>>
>> > Regards
>> > Marco
>> > The Netherlands
>
>Cor,
>
>How would that code look like, I have no idea.
>
>MArco


For Each dr As DataRow In ds.Tables(0).Rows
     For Indx As Integer = ListBox1.Items.Count-1 TO 0 STEP -1
         Dim mlist As MyList = DirectCast(ListBox1.Items(Indx),
MyList)
         If dr.Item("folder") = mlist.itemdata Then
            ListBox2.Items.Add(mlist)
            ListBox1.Items.Remove(mlist)
         End If
     Next
Next