How to access list from another form

前端 未结 2 1399
青春惊慌失措
青春惊慌失措 2021-01-07 08:04

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

2条回答
  •  借酒劲吻你
    2021-01-07 08:30

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

    public partial class Form2 : Form
    {
        public void SetAllPeople(List 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();
    

提交回复
热议问题