1 Replies - 5488 Views - Last Post: 21 November 2009 - 11:32 AM Rate Topic: -----

#1 nkasei28  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 93
  • Joined: 10-May 09

Add all items from listbox into another listbox

Posted 21 November 2009 - 11:13 AM

Hi there, i have a list of items in a listbox and i want to move all of them into another listbox. I'm using a loop to do this but it always generates an exception anytime the loop's counter equals 2. Currently there are only three items in the former listbox. The first and last items are moved correctly except the second. Please help me fix this. Thanks a bunch. Here is my code:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
		Dim itemsCount As Integer = Me.lsbAbsent.Items.Count
		Dim i As Integer
		Dim selected As Object

		Try
			'At i=2, it generates an exception
			For i = 0 To itemsCount - 1 
				selected = Me.lsbAbsent.Items.Item(i)
				Me.lsbPresent.Items.Add(selected)
				Me.lsbAbsent.Items.Remove(selected)
			Next i
		Catch ex As Exception
			MsgBox(ex.Message)
		End Try

	End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Add all items from listbox into another listbox

#2 LeisureProgrammer  Icon User is offline

  • N00by VB.NET Developer
  • member icon

Reputation: 110
  • View blog
  • Posts: 588
  • Joined: 06-September 09

Re: Add all items from listbox into another listbox

Posted 21 November 2009 - 11:32 AM

Your code seems a bit unnecessarily complicated. Let me try and see what was going wrong in your code, meanwhile, here's some simpler code:

For ttt As Integer = 0 To lsbabsent.Items.Count - 1
			lsbpresent.Items.Add(lsbabsent.Items.Item(ttt))
		Next
		lsbabsent.Items.Clear()



Ohhhh, I get it:

You were using a for loop, i kept increasing... i should remain the same, this is because the item at index 0 will always be the next item (because you are also removing items as you go)

		  While lsbabsent.Items.Count <> 0
				selected = Me.lsbabsent.Items.Item(0)
				Me.lsbpresent.Items.Add(selected)
				Me.lsbabsent.Items.Remove(selected)
			End While



If you don't understand what was going on, remove all the code from your button event, and add this code instead, and run it...

 Dim itemsCount As Integer = Me.lsbabsent.Items.Count
		Dim i As Integer
		Dim selected As Object

	 
		selected = Me.lsbabsent.Items.Item(0)
		Me.lsbpresent.Items.Add(selected)
		MsgBox("Item added: " & selected.ToString)
		Me.lsbabsent.Items.Remove(selected)
		MsgBox("Item removed: " & selected.ToString)

		selected = Me.lsbabsent.Items.Item(1)
		Me.lsbpresent.Items.Add(selected)
		MsgBox("Item added: " & selected.ToString)
		Me.lsbabsent.Items.Remove(selected)
		MsgBox("Item removed: " & selected.ToString)

		MsgBox("see, there is no item at index 2")

		selected = Me.lsbabsent.Items.Item(2)
		Me.lsbpresent.Items.Add(selected)
		MsgBox("Item added: " & selected.ToString)
		Me.lsbabsent.Items.Remove(selected)
		MsgBox("Item removed: " & selected.ToString)



This post has been edited by LeisureProgrammer: 21 November 2009 - 11:47 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1