Hello everyone..i am newbie in C# programming.. i have two different forms..Form 1 and Form 2. Users will select multiple items in form 1. i wanted to duplicate the listbox in form 1 to form 2. please suggest how to do..

Dani AI

Generated

— glad you sorted it out. For anyone else reading: there are two clean, maintainable patterns for duplicating a ListBox from one form to another. Either copy the data (items) and populate the second ListBox, or share a data model and bind both controls to it. Avoid reaching into another form's controls directly; expose a constructor, property, or method on the target form that receives a collection.

A minimal WinForms pattern (pass an array of objects to the second form):

/* In Form2 */
public Form2(object[] items)
{
    InitializeComponent();
    if (items != null) listBox2.Items.AddRange(items);
}

/* In Form1 */
using System.Linq;
var items = listBox1.Items.Cast<object>().ToArray();
var f2 = new Form2(items);
f2.Show();

If you prefer a shared model so both lists stay in sync, bind to a BindingList<T> (or ObservableCollection for WPF) and pass that collection to Form2 instead of copying items.

For ASP.NET WebForms (different page), extract the texts/values and transport them via Session, QueryString (small payloads only), or Cross-Page techniques, then rebuild the ListBox on the target page. Example sketch:

/* Page1 */
var texts = listBox1.Items.Cast<System.Web.UI.WebControls.ListItem>().Select(li => li.Text).ToArray();
Session["CopiedList"] = texts;
Response.Redirect("Page2.aspx");

/* Page2 Page_Load */
var texts = Session["CopiedList"] as string[];
if (texts != null) foreach (var t in texts) listBox2.Items.Add(new System.Web.UI.WebControls.ListItem(t));

Notes and cautions: preserve selected indices separately if you need selection state; if items are custom objects prefer passing the model (not merely strings) or clone objects to avoid shared-reference bugs; and if forms run on different threads use Invoke when updating UI. ’s tutorial pointed in the right direction — these patterns make the transfer robust and testable.

Recommended Answers

All 3 Replies

check my new tutorial since this seems to be a common problem :)

Thanks for the reply.. i am adding them in my code but i am dealing with list boxes. i wanted to copy all the items from one list box to another.

get { return textBox1.Text; } can not be applied to listboxes. can you suggest me which property to use?

please bear with me as i am very new to programming :(

thank you its working

Be a part of the DaniWeb community

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