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 <<

Recommended Answers

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

Jump to Post

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

Jump to Post

All 7 Replies

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

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

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

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

wow thnx geek works like magic

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.

i did not use any datasource

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.