Reputation: 23
I want to copy listbox items from one form to another....actually in the 1st form I have 2 listboxes and in the 2nd form I also have 2 listboxes. I want to move the items of the 1st form's listboxes to 2nd form..... please help me....
Upvotes: 2
Views: 14525
Reputation: 1323
public partial class Form1 : Form
{
List<String> mylistSource;
public Form1()
{
InitializeComponent();
mylistSource = new List<string>();
// populate source with test data
for (int i = 0; i < 25; i++)
{
mylistSource.Add(i.ToString());
}
//assign source to both lists
listBox1.DataSource = mylistSource;
listBox2.DataSource = mylistSource;
}
}
Just add 2 listboxes to a form and paste in code to run.
or if you just want to copy selected items you can simply do this:
foreach (var item in listBox1.SelectedItems)
{
listBox3.Items.Add(item);
}
Upvotes: 3
Reputation: 73163
If you want to select the entire items from listbox1 to listbox2, then the easiest, most readable and fastest have to be:
listbox2.Items.AddRange(listbox1.Items);
Upvotes: 4
Reputation: 9986
In case of web form, use session to forward the data source of the list.
Upvotes: 1
Reputation: 31174
what you could do, is give the objects an extra property (eg. selected)
you bidn the collection to both the listboxes, but in the one you only show the ones with selected = false and in the other selected = true
and if you "move" the item, you just need to switch selected to true and refresh the ItemsSources
Upvotes: 0