unable to remove items from Listbox

  • Thread starter Thread starter Co
  • Start date Start date
C

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
 
Co said:
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
 
Co said:
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
 
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
 
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
 
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,

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,

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

Cor,

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

MArco
 
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

Cor,

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

MArco
 
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
 
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
 
Back
Top