0

I have 3 forms: form1(which I want to use my List from form3 in which I create List and Add things to it), form2 ( which contains a button to go back to form1 and a button to go to form3 and get values to the list.

I tried creating the following class:

public class ListArticle
    {
        public List<string> Clothes { get; private set; }
        public List<string> Colors { get; private set; }

        public ListArticle()
        {
            Clothes = new List<string>();
            Colors = new List<string>();
        }
    }

and then declare trying Adding things in the List from form3 like this:

// This is the Declaration

public ListArticle _articles = new ListArticle();

    public ListArticle Articles
    {
        get
        {
            return _articles;
        }
        set
        {
            _articles = value;
        }
    }

This is how I add:

_articles.Clothes.Add("T-shirt " + tshirt_number.ToString());
_articles.Colors.Add(closestColor2(clist, color));

and this is how I am trying to get the values:

when I close form3

I do this:

Form2 frm = new Form2();
frm.Show();
Articles = _articles;
this.Hide();

in form2 I do nothing..

and in form1 I tried to do it like this:

//declaration

public ListArticle Articles;

public ListArticle _articles
{
   get
   {
     return Articles;
   }
   set
   {
     Articles = value;
   }
}

//and this is how I tried to do it but it returns null everytime.

private void button3_Click(object sender, EventArgs e)
    {
        try
        {
            Form3 f = new Form3();

            f.Articles = Articles;

            foreach (string c in Articles.Clothes)
            {
                MessageBox.Show(c);
            }
        }
        catch 
        {
            MessageBox.Show("Articles is null.");
        }

    }
C. Cristi
  • 569
  • 1
  • 7
  • 21
  • 1
    [Interaction between forms — How to change a control of a form from another form?](https://stackoverflow.com/questions/38768737/interaction-between-forms-how-to-change-a-control-of-a-form-from-another-form) – Reza Aghaei Dec 28 '16 at 21:11
  • You asked a similar question yesterday which is still open. You need to learn more about how to communicate between forms, so spend some time to read the linked post. – Reza Aghaei Dec 28 '16 at 21:13
  • @RezaAghaei I know but i felt like I gave too less information about what I really wanted to ask and to remake a full question with answers is bad. – C. Cristi Dec 28 '16 at 21:14
  • @RezaAghaei also .. I tried to study this all day but couldn't figure it out and now I am just sad – C. Cristi Dec 28 '16 at 21:15
  • `Form3 f = new Form3(); f.Articles = Articles;` → 1) You created a new instance of `Form3` 2) You assigned `Articles` of `Form1` to `Articles` of `Form3`. 3) In `Form1` you didn't instantiate `Articles` so Obviously its null. – Reza Aghaei Dec 28 '16 at 21:23
  • @RezaAghaei for me looks like the opposite I assigned the `Articles` of `Form3` to `Articles` of `Form1`... – C. Cristi Dec 28 '16 at 21:30
  • When you say `x=y` which one gets the value and will change? `x` or `y`? `f.Articles = Articles;` is equivalent to `f.Articles = this.Articles;` which means you are assigning `Articles` of current form to `Articles` of `f` which is a new instance of `Form3`. – Reza Aghaei Dec 28 '16 at 21:31

1 Answers1

0

If you want to be able to share the articles between all forms you could make the Clothes and Colors collection static:

public class ListArticle
{
    public static List<string> Clothes { get; private set; }
    public static List<string> Colors { get; private set; }

    static ListArticle()
    {
        Clothes = new List<string>();
        Colors = new List<string>();
    }
}

You can then add articles form one form like this:

ListArticle.Clothes.Add("T-shirt " + tshirt_number.ToString());
ListArticle.Colors.Add(closestColor2(clist, color));

...and retrieve articles from another form like this:

private void button3_Click(object sender, EventArgs e)
{
    try
    {
        foreach (string c in ListArticle.Clothes)
        {
            MessageBox.Show(c);
        }
    }
    catch
    {
        MessageBox.Show("Articles is null.");
    }
}

Using this approach you don't have to create any additional "article" properties in either of the forms. You just access the same static collections from all forms.

mm8
  • 163,881
  • 10
  • 57
  • 88