954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Moving 2 Listbox

here i have 2 listbox i already have the code for the selected item moving right and moving left but i don't know how to move all of the listbox1 to listbox2 by just click >> or listbox2 to listbox1 <

Attachments untitled.JPG 9.6KB
Shodow
Junior Poster
115 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
 

Just use looping.
If you already manage to move single item, then use looping to move all items.

Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

i dont know much of looping..can you teach me?

Shodow
Junior Poster
115 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
 

Hi,

Try with this,

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
Dim i, j As Integer
i = ListBox1.Items.Count()
For j = 0 To i - 1
ListBox2.Items.Add(ListBox1.Items(j))
Next j
ListBox1.Items.Clear()
End Sub

kothaisaravan
Junior Poster in Training
51 posts since Oct 2011
Reputation Points: 10
Solved Threads: 2
 

Actually if you want to transfer ALL items from one listbox to another then you can simple do that like the sample below. No looping needed.

Private Sub MoveToBox2_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
		ListBox2.Items.Clear()
		ListBox2.Items.AddRange(ListBox1.Items)
		ListBox1.Items.Clear()
	End Sub

	Private Sub MoveToBox1_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
		ListBox1.Items.Clear()
		ListBox1.Items.AddRange(ListBox2.Items)
		ListBox2.Items.Clear()
	End Sub
GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

wow thnx geek works like magic

Shodow
Junior Poster
115 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
 

The above sample code is ONLY for the "<<" and ">>" buttons, no selection required. It takes ALL items from listbox1 and moves it to listbox2. But there is a leak...if a user already moved some items from box1 to box2 then those items are lost.
So the question is, do you get the listbox items froma datasource? if so, please tell us from what datasource, so we can adjust the example.

GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

i did not use any datasource

Shodow
Junior Poster
115 posts since Jan 2011
Reputation Points: 14
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: