1

I'm trying to fill a List<String> from another form while both forms are open.

In the form WorkOrder i have a list: List<String> materialsList, then in my other form AddMaterials i have other List<String> list, this is the list that i want to pass to the WorkOrder form, but both forms are open and i don't know it is possible to do that.

Here are my forms:

enter image description here

If i click the Add Materials button in the WorkOrder form then the form AddMaterials opens. Now with the form AddMaterials open, i fill one by one the elements of the list with the button Add new Material, then when i click finish i want to pass the List<String> list to the WorkOrder list: List<String> materialsList.

This is the code that i'm trying to solve this:

In the WorkOrder form:

List<String> materialsList = new List<string>();

public void setList(List<String> l)
{
    this.materialsList = l;
}

In the AddMaterials form:

public partial class AddMaterials : Form
{

    List<String> list = new List<string>();

    public AddMaterials()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //Adding Materials
        list.Add(material.Text);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //Passing the list with Method setList()
        WorkOrder o = new WorkOrder();
        o.setList(list);
    }
}

Any question post on comments.

TimeToCode
  • 901
  • 2
  • 16
  • 34
  • [Interaction between forms — How to change a control of a form from another form?](http://stackoverflow.com/a/38769212/3110834) – Reza Aghaei Oct 01 '16 at 17:59

2 Answers2

1

You could pass a reference to either the WorkOrder Form or the WorkOrder.materialsList in the constructor of your AddMaterials Form. So your AddMaterials code could be

public partial class AddMaterials : Form
{
    WorkOrder wo;

    public AddMaterials(WorkOrder wo)
    {
        InitializeComponent();
        this.wo = wo;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //Adding Materials
        this.wo.materialsList.Add(material.Text);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
    }   
}

In WorkOrder you'd have this function which opens a new AddMaterials Form. You could place in logic here before you create a new AddMaterials Form to clear the list or do something else if you have multiple materials list for a WorkOrder.

private void button1_Click(object sender, EventArgs e)
{
    // Add logic here to deal with multiple AddMaterial Forms 
    // on one work order
    new AddMaterials(this).Show();
} 
Josh
  • 2,767
  • 1
  • 27
  • 31
0

What you're doing is not working since you are using two completely different lists. You can create a custom constructor for AddMaterials:

public AddMaterials(List<string> materials)
{
    InitializeComponent();
    this.materials = materials;
}

and declaring materials as a member of AssMaterials class:

private List<string> materials;

If you want to keep things even more separated you can pass a delegate:

public delegate void Del(string str);

class AddMaterials
{
    private Del addMaterialDelegate;

    public AddMaterials(Del AddMaterialDelegate) : Form
    {
        InitializeComponent();
        this.addMaterialDelegate = AddMaterialDelegate;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //Adding Materials
        addMaterialDelegate(material.Text);
    }
}

and then from WorkOrder:

    private void button1_Click(object sender, EventArgs e)
    {
        new AddMaterials(OnAddMaterialDelegate).Show();
    }

    private void OnAddMaterialDelegate(string material)
    {
        this.materialsList.Add(material);
    }

This will allow you to change your mind in the future without too much rework (for example if you want to switch from a List to a different type).

peval27
  • 1,239
  • 2
  • 14
  • 40