Click here to Skip to main content
15,867,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two forms. I want listbox on the first form to show its result to listbox on the second form. Need some assistance on how to do that.
Posted
Comments
Richard MacCutchan 10-May-14 14:08pm    
What do you mean by "results" in this context? A listbox is merely a list that can be displayed.
Member 10807798 10-May-14 14:12pm    
I mean display on the other listbox. When I add data in the first listbox on the first form, it doesn't display on the second listbox on the second form
BillWoodruff 10-May-14 19:24pm    
Are you saying that you want the ListBox Items on a second Form to always exactly be the same as the ListBox Items on a first Form ? So: you add an Item to the first Form, and the second Form gets the same Item added, etc. ?
Member 10807798 10-May-14 22:20pm    
Yes!
[no name] 10-May-14 14:13pm    
tell us clearly and what have you tried yet

1 solution

In C# you can't access to other Forms' Controls, but we know that static Members are accessible through other classes. so you should define a static variable in first form to store items in it like this:
In First form decalre the following
C#
public static List<string> myList = new List<string>();

Next in an event of first form add the Listbox(listbox1) items in List
C#
for (int i = 0; i < listBox1.Items.Count; i++)
     myList.Add(listBox1.Items[i].ToString());

and in second form you can access this variable by the following and add to listbox2 of second Form
C#
for (int i = 0; i < Form1.myList.Count; i++)
     listBox2.Items.Add(Form1.myList[i]);
 
Share this answer
 
v4
Comments
Member 10807798 10-May-14 14:29pm    
I try putting the code for the second form, but it running an error saying

Form1.myList say an object reference is require
listbox1 does not exist
Sayan Bera 10-May-14 14:32pm    
Where are you declaring the List myList? Does your second form contain listbox control named listbox1??
Sayan Bera 10-May-14 14:38pm    
I'm putting the whole code .....Try this out.
First Form
-----------
namespace CodeProject
{
public partial class Form2 : Form
{
public static List<string> myList = new List<string>();

public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
Form3 fm3 = new Form3();
fm3.Show();

for (int i = 0; i < listBox1.Items.Count; i++)
myList.Add(listBox1.Items[i].ToString());
}
}
}
------------------------------------------------------------------
Second Form
-----------
namespace CodeProject
{
public partial class Form3 : Form
{

public Form3()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < Form2.myList.Count; i++)
listBox1.Items.Add(Form2.myList[i]);
}
}
}
Member 10807798 10-May-14 14:42pm    
In the second it just Form1.myList.Count and Form1.myList[i] that running the error.
Sayan Bera 10-May-14 14:47pm    
I guess you haven't declared the myList as Static!

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