0

I generated a list on form1 who's contents are numbers and a letter in the form of K000-000. I then want to open form2. On form 2 I have a text box, a list box and a button. In the text box you will type some more number in like 12345. When you click the button I want it to add the contents of form1's list with a "-" on the end and the contents you typed in Form2's textbox. So the listbox will be K000-000-12345. I'm not sure how to properly use Form1's list on Form2 and also add to it.

Form1

 DesignNo.FindByItem(electype, (int.Parse)(dwgno));  

            List<DesignNo> Electrode = DesignNo.FindByItem(electype, (int.Parse)(dwgno));

            if (Electrode.Count <= 0)
            {
                MessageBox.Show("Unknown Electrode Number");
            }

            frmElectrode frmelec = new frmElectrode();
            frmelec.Show();

frmelec being Form2 in the example.

Lee
  • 95
  • 1
  • 8
  • Add a constructor to `Form2` that takes a `List` as an argument. When you create your `Form2`, pass the `List` instance in as an argument there. `Form2` would also need a property to store the list, to be accessed by `Form1`, or passing it by reference should work too. Just search around for passing objects / values between forms; this has been asked countless times. – sab669 Sep 02 '15 at 19:07
  • 1
    [Duplicate 1](http://stackoverflow.com/questions/3062575/) [duplicate 2](http://stackoverflow.com/questions/7800731/) [duplicate 3](http://stackoverflow.com/questions/17032484/) [duplicate 4](http://stackoverflow.com/questions/17836398/) [duplicate 5](http://stackoverflow.com/questions/25316230/) [duplicate 6](http://stackoverflow.com/questions/29092707/) ... – Dour High Arch Sep 02 '15 at 19:38

3 Answers3

2

1-Using Static property

  public static List<int> list;


    public Form1()
    {
        list=new List<int>();
        InitializeComponent();
    }

in Form2 access list like this

Form1.list.Add(item);

2-using constructor

    public static List<int> list;
    public Form1()
    {
        list=new List<int>();
        InitializeComponent();
    }
   public void ShowForm2()
    {
     var form2=new Form2(List);
     form2.show();
    }

in Form2

    public partial class Form2 : Form
    {
    public Form2()
    {
     InitializeComponent();
    }

    public static List<int> _list;
    public Form2(List<int> list)
    {
        _list=list;
        InitializeComponent();
    }
    }
Arash
  • 861
  • 7
  • 17
  • i think all your approaches create a dependency into the wrong direction. No form should know the mainform. Better would be a public property on form2 that is added to the list after form 2 closes. – quadroid Sep 02 '15 at 19:25
2

create a public property inside Form 2

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public string Content { get; private set; }

    public void ButtonOkOnClick()
    {
        this.Content = this.textBox1.Text;
        this.Close();
    }
}

Consume the public property in Form1 after Form2 gets closed

    public Form1()
    {
        InitializeComponent();

        Form2 form2 = new Form2();
        form2.Show();
        form2.Closed += (sender, args) => this.list.Add(form.Content);
    }

This way the dependency is the right direction, form2 is just a input form and can be reused without any dependencies.

quadroid
  • 7,621
  • 5
  • 41
  • 71
0

A clean way to share some state (in your case dictionary of keys) is via some shared service (singleton), so:

You could create a class (e.g. ElectrodeManager) which will hold a dictionary of electrodes (empty in the first place).

In the Form1 you will populate that dictionary via specified method on that class (e.g. AddElectrode(string electrodeType, string electrodeKey) -> which will add new item into the dictionary) - so you will have Dictionary<string, string> that holds e.g. {"T1", "K000-000"}, {"T2", "K000-0001"} ...

In the Form2 you will work that dictionary from the ElectrodeManager and you will append string from the textbox onto electrode's key.

Example:

public class ElectrodeManager
{
    #region Singleton Pattern

    private static ElectrodeManager instance;
    public static ElectrodeManager Instance
    {
        get
        {
            if (instance == null)
                instance = new ElectrodeManager();
            return instance;
        }
    }

    private ElectrodeManager()
    {
        electrodes = new Dictionary<string, string>();
    }

    #endregion

    #region Fields

    private Dictionary<string, string> electrodes;

    #endregion Fields

    #region Methods

    public void AddElectrode(string eType, string eKey)
    {
        if (!electrodes.ContainsKey(eType))
        {
            electrodes.Add(eType, eKey);
        }
    }

    public void AppendStringToElectrodeKey(string eType, string keyAddendum)
    {
        string electrodeKey = String.Empty;
        if (electrodes.TryGetValue(eType, out electrodeKey))
        {
            electrodes[eType] = String.Format("{0}-{1}", electrodes[eType], keyAddendum);
        }
    }

    public IDictionary<string, string> GetElectrodes()
    {
        return electrodes;
    }

    #endregion Methods
}

Usage inside Form1 (somewhere in generation logic):

ElectrodeManager.Instance.AddElectrode("T1", "K000-000");

ElectrodeManager.Instance.AddElectrode("T2", "K000-001");

Inside Form2 (button click):

ElectrodeManager.Instance.AppendStringToElectrodeKey("T1", textBox.Text);

ElectrodeManager.Instance.AppendStringToElectrodeKey("T2", textBox.Text);

Of course, you could easily switch data type to the List<string> if that suits you better.

Darjan Bogdan
  • 3,535
  • 1
  • 18
  • 29
  • read more about why singletons are so bad here http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons – quadroid Sep 02 '15 at 20:18
  • I agree that this implementation isn't quite good and probably should be avoided. However it would be unecessary overhead if I introduced IoC/DI with an object creation in a singleton scope and constructor injection in the both forms. Also, I've done it via singleton since I wanted to have centralised list of keys (storage), instead of two seperate lists. – Darjan Bogdan Sep 02 '15 at 20:43