1

Using Visual Studio 2015 and C#

I have two forms, Form1 and Form2 that are part of the same project in Visual Studio. Within Form1, I build a list of strings, based on selections made in check boxes. I need to be able to iterate through this list in Form2. However, I am having trouble calling the list (list1) in Form2, when it is built and updated in Form1.

Form2 gets called in Form1 within a button click, seen below:

  public void BTN_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();

Looking for any tips to be able to access list1 in Form2. Thanks!

  • There are a variety of ways to allow a pair of `Form` classes to interact with each other. The best methods avoid coupling the two classes (i.e. making them both know about each other...one knowing about the other is fine), and avoid exposing the actual controls (use events and properties that wrap properties of controls and other objects instead). The marked duplicate provides a lot of good information on the topic. – Peter Duniho Mar 22 '17 at 21:07
  • If after you've reviewed the information and tried to use it, you have a _specific_ problem, post a new question with a good [mcve] showing exactly what you've tried, and explain what the code does and what you want it to do instead. – Peter Duniho Mar 22 '17 at 21:07

1 Answers1

0

You can simply make make a property like:

   public List<string> List1 { get {return list1;} set { list1 = value;}}

and then access it from other forms:

Form1 frm1 = new Form1();
List<string> list = form1.List1;
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171