Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
1.18/5 (4 votes)
See more:
can someone tell me how can i move all the items fro one listbox to another listbox
Posted
Updated 16-Aug-17 9:53am
Comments
koolprasad2003 8-Dec-11 7:57am    
Remove uppercase formatting. Uppercase indicates Shouting.

try this..

On Button Click..

C#
ListBox2.Items.Clear();
foreach(ListItem item in ListBox1.Items)
{
     ListBox2.Items.Add(item);
}


hope it helps..
 
Share this answer
 
Comments
LanFanNinja 8-Dec-11 7:45am    
This will work but there is a better way (see my solution). I only voted you 4 because the OP is using VB and your solution is C#.

--EDIT--
Changed vote from 4 to 3 see my comment to Mark Nischalke below for the reasons why.
[no name] 8-Dec-11 7:58am    
The language doesn't matter and voting because of it is nitpicking and petty. There will not always be examples in the language of your choice.
LanFanNinja 8-Dec-11 8:10am    
Ok well ignoring the down vote for using the wrong language I down voted him yet another star (from 4 to 3) because after trying his code myself I found it to be no good! The reasons why are posted below.

There is no "ListItem" perhaps he meant "ListViewItem" well that is no good either and running the code that way I obviously receive a "Unable to cast
object of type 'System.String' to type 'System.Windows.Forms.ListViewItem'." exception!

The correct code would be something like this
foreach (string item in listBox2.Items)
{
listBox1.Items.Add(item);
}
LanFanNinja 8-Dec-11 8:32am    
And yes language does matter! Judging by the question posted I assumed the OP to be fairly new to programming and most likely only knows VB.Net. So I felt that giving him or her code in C# would only help to confuse them.

Imagine if you will that you speak only Chinese and I speak only Japanese
sure we both speak Asian languages but we still cannot understand each other.
Even easier:

C#
listbox2.Items.AddRange(listbox1.Items.ToArray());

EDIT ========================

C#
listbox2.Items.AddRange(listbox1.Items.ToArray());


The lesson here is "be a programmer and muscle through this kind of thing..."
 
Share this answer
 
v2
Comments
LanFanNinja 8-Dec-11 7:42am    
ListBox.ObjectCollection contains no method ToArray()
Try this

VB
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    'uncomment this line below only if you want to clear the items
    'from ListBox1 before adding the items from ListBox2
    'ListBox1.Items.Clear()

    ListBox1.Items.AddRange(ListBox2.Items)
End Sub
 
Share this answer
 
v3
Comments
[no name] 8-Dec-11 7:56am    
A repeat of JSOP's post
LanFanNinja 8-Dec-11 8:02am    
NO it is NOT! Please read both solutions again. You will notice there is an error in his code I pointed out and my solution is in VB.Net (the language the OP is using).

Please understand I do not make it a habit to waste my time posting solutions to questions that have already been correctly answered. I felt this question was NOT correctly answered so I offered my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900