Currently I am sending one Array List from form2 to form 1 and it works fine.
Form1 form2 = new Form1(this, SampleArrayList); //pass form reference and an arraylist
form2.Show();
this.Hide();
And on form1 I associate SampleArrayList with local Array List.
Form2 formParent;
ArrayList SampleArrayList;
public MainForm(Form2 par, ArrayList _SampleArrayList)
{
InitializeComponent();
this.formParent = par;
this.SampleArrayList = _SampleArrayList;
}
However I want to avoid creating new instance of Form1
form2 = new Form1(this, SampleArrayList);
I want to send Array List to currently running instance of Form1. What would be best way to do this. Thank you
Quote of OP in the comments above:
That’s really a larger problem then. Here’s a nice solution that I like.
A few notes:
ArrayList, rather than strings as I did inthis example. An
ArrayListis a mutable reference type, whereasstringis immutable. This means that you can just modify theArrayListpassed to Form2, and those changes will be reflected inthe variable in Form1 since they both point to the same underlying
ArrayList. I left this code in here as it covers the general casethough.
Normally in this design you would close it since it won’t be needed
anymore. If you really don’t plan to use it again I suggest closing
it. If you really do plan to show that form again then just hiding
it is fine.
you just hide it it will fire the Visible event. You should probably
remove one of those two event handlers in my code depending on
whether you actually close it or hide it. If you sometimes do one and
sometimes the other then feel free to leave both. You won’t actually
harm anything (other than confusing people) if you leave both in even
if you only use one.