How to access list from another form

11,346

Solution 1

Change this:

 Form2 lista = new Form2();

to something like this:

 Form2 lista = new Form2(allPeopleSource);

Remove this from form2:

 Form1 main = new Form1();

And put this code in form2

List<string> allPeopleSourceInForm2;

Public Form2(List<string> allSourcesAsParameter)
{
 allPeopleSourceInForm2 = allSourcesAsParameter;
}

Now your code should work. What we are doing here, is bringing reference of a list to Form2. Form1 contains all the info and we are just passing address of this data to Form2. This reference is brought to Form2 as an constructor parameter. In Form2 constructor we can do what ever we want with data, but be noticed that all the changes into collection at form2 will affect also collection in Form1.

Solution 2

You can create a method to populate the list on Form2 (I called it SetAllPeople):

public partial class Form2 : Form
{
    public void SetAllPeople(List<string> input)
    {
        foreach (string s in input)
        {
            lsbResidents.Items.Add(s);
        }
    }

    public Form2()
    {
        InitializeComponent();
    }
}

and then call Form2 this way:

Form2 lista = new Form2();
lista.SetAllPeople(allPeopleSource);
lista.ShowDialog();
Share:
11,346
linuxbegin
Author by

linuxbegin

Updated on June 04, 2022

Comments

  • linuxbegin
    linuxbegin almost 2 years

    I have two forms, in Form1 a create objects from a class and puts into list (list of objects). then I have another list of strings which is supposed to be a source for Form2 - the list of strings contains elements which are object's attributes turned into strings. Form2, after opening, should read the data from the list of strings and put them into listbox. The problem is it does not see the data. I can access data in Form1, so I'm sure the list is populated, but Form2 cannot access the data - how to solve this?

    I have tried different ideas taken from the forum and other sources but none of them worked for me. what worked was populating the list which is supposed to be a data source in Form1 'initialize component' but it is not a perfect solution since objects are created after clicking a button. now I put the list at the top of the Form1 class but it is still populated by clicking a button - and it does not work, the listbox is still empty.

    Here is the code of Form1:

    public partial class Form1 : Form
    {
        public House MyHouse = new House();
        public List<string> allPeopleSource = new List<string>();
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            Person p1 = new Person("Simon", 33);
            Person p2 = new Person("Peter", 23);
    
            MyHouse.IsInhabitedBy(p1);
            MyHouse.IsInhabitedBy(p2);
    
            allPeopleSource.Add(p1.Name + " | " + p1.Age.ToString());
            allPeopleSource.Add(p2.Name + " | " + p2.Age.ToString());
    
            Form2 lista = new Form2();
            lista.ShowDialog();
        }
    }
    

    Here is the code of Form2:

    public partial class Form2 : Form
    {
        Form1 main = new Form1();
        List<string> allPeople = new List<string>();
    
        public Form2()
        {
            InitializeComponent();
    
            foreach (string s in main.allPeopleSource)
            {
                allPeople.Add(s);
            }
    
            foreach (string s in allPeople)
            {
                lsbResidents.Items.Add(s);
            }
        }
    }
    

    I appreciate any help.

  • Panu Oksala
    Panu Oksala over 10 years
    There is a bug in example. You are populating lbsResidents in constructor, but calling SetAllPeople after constructor. To correct this you can actually just populate lbsResidents in SetAllPeople method and forget the allPeople variable: public void SetAllPeople(List<string> input) { foreach (string s in allPeople) { lsbResidents.Items.Add(s); } }