|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
unable to remove items from ListboxI 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
Show quote
Hide quote
"Co" <vonclausow***@gmail.com> wrote in message I am surprised that you didn't get a compile error stating 'that you cannot 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 > 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 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 > 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
Show quote
Hide quote
On 20 mei, 10:03, "Cor Ligthert[MVP]" <Notmyfirstn***@planet.nl> Cor,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 How would that code look like, I have no idea. MArco 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> For Each dr As DataRow In ds.Tables(0).Rows>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 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
copying folders
error when updating Listview after record add Can you edit auto generated Partial Class Designer VB code? create reference number based on old one... Detecting Design Time vs Run Time Property Set action Send Email Using VB 2008 Express Vb.net[2008] Combo box population from text file (40,000 lines!) Radio buttond and combo boxes newbie setup question print as user? |
|||||||||||||||||||||||