如何从另一种形式的访问列表 [英] How to access list from another form

查看:112
本文介绍了如何从另一种形式的访问列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式,在Form1上创建一个类的对象并投入列表(对象的列表)。然后,我有这应该是 Form2的源字符串的另一份名单 - 字符串列表中包含有对象的属性变成字符串元素。 窗体2 ,开封后,应读字符串列表中的数据,并把它们放到列表框。问题是,它没有看到数据。我可以在 Form1中访问数据,所以我敢肯定,填充了名单,但窗体2无法访问数据 - 如何解决这个

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?

我试图从论坛和其他来源采取了不同的想法,但他们没有为我工作。什么工作是填充它应该是在Form1中的数据源列表中选择初始化组件,但它不是,因为点击一个按钮后创建对象完美的解决方案。现在我把名单在Form1类的顶部,但它仍然是通过点击一个按钮填充 - 和它不工作,列表框仍然是空的。

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.

下面为 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();
    }
}

下面是的代码窗体2

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.

推荐答案

更改此:

Change this:

 Form2 lista = new Form2();

要像这样:

to something like this:

 Form2 lista = new Form2(allPeopleSource);



从窗口2删除此:

Remove this from form2:

 Form1 main = new Form1();

和放在窗体2这段代码

And put this code in form2

List<string> allPeopleSourceInForm2;

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

现在您的代码应工作。我们在这里做的,是把一个列表,Form2的参考。 Form1中包含了所有的信息,我们只是通过这个数据到窗体2的地址。该参考被带到窗体2作为一个构造函数的参数。在Form2的构造函数中,我们可以做什么都我们希望与数据,但必须注意到,在所有Form 2上变为集合还会影响收集Form1上。

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.

这篇关于如何从另一种形式的访问列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆