I have two forms: Form1 and Form2. I can get the contents of a List in Form1 to another List in Form2 by calling a new Form2 and passing the values in. I update the List in form2 by say, removing an item from it. How would I pass the contents of Form2's list BACK to the original List in Form1? Form1 is the first form that appears when the application runs, so I don't want to call a new instance of the form.
A:
You can pass the initial instance of Form1
to Form2
and use this reference to pass data back to Form1
.
// A property `Form2`
public Form1 RefToForm1 { get; set; }
// On form 1, after initializing `Form2`:
Form2 frm2 = new Form2();
frm2.RefToForm1 = this;
Note:
There are better solution than the above (it is quick and dirty). A better option would be to create a property on Form2
with the type of data you need in Form1
and access the data through it:
// A property `Form2`
public List<int> Form2DataForForm1 { get; set; }
// On form 1
var dataFromForm2 = frm2.Form2DataForForm1;
Oded
2010-10-10 20:27:22
I find that this solution creates a two-way dependency between Form1 and Form2. I'd rather avoid having Form2 depend on Form1; see my answer on this page.
CesarGon
2010-10-10 20:34:15
Thank you this worked an absoloute charm. Many thanks again.
Jon Mattias
2010-10-10 20:35:32
@CesarGon - The dependency is already there. Making it bi-directional does not make things any worse (in my opinion), and makes the relationship more explicit (the fact that data needs to travel both ways).
Oded
2010-10-10 20:38:55
@Oded: I don't think the dependency is there unless you code for it. In my proposed solution, you can delete one class and the other one doesn't need to be touched; in your solution, this is not the case. In other words: in your solution, Form2 mentions the Form1 type, and that matters to the compiler and run-time (especially relevant if they happened to be in separate assemblies!). That does not happen in my solution.
CesarGon
2010-10-10 20:40:54
It would be better to pass a reference type for data exchange (C# class), since changes made by the child form will be visible to the parent, and then use events instead of having the child directly invoke any methods on the parent.
Ben Voigt
2010-10-10 20:41:57
@CesarGon, @Ben Voigt - fair points. Answer updated with better solution.
Oded
2010-10-10 20:56:45
@Oded: Well, I see that you have pinched my solution. They say that imitation is the best form of flattery. :-) Pity that the question author hasn't realised this and has awarded the answer to you.
CesarGon
2010-10-11 15:57:51
@CesarGon - In my experience, the use of code samples is what made the difference... SO is specifically made so answers can be updated and made better. I hope you agree.
Oded
2010-10-11 18:01:05
@Oded: No, I don't agree, sorry. Updating is fine. Pinching somebody else's solution so blatantly is not on, especially when *I* was the person who helped *you* perfect your answer! But don't worry, this page shows everything, so let's let people judge. :-)
CesarGon
2010-10-11 18:02:59
+2
A:
I suggest you implement a property in Form2 that returns the relevant data, and have Form1 read that property, "pulling" the data from Form2.
This is better than having Form2 "push" the data back into Form1, since it keeps the dependencies one way only.
CesarGon
2010-10-10 20:28:19
Neither pushing nor pulling is inherently better. For pull, use properties as Cesar describes. For push, use events.
Ben Voigt
2010-10-10 20:43:07
@Ben: I agree, neither pushing nor pulling is inherently better. But a uni-directional dependency is better than a bi-directional one. Pushing through an event can keep dependencies one-way only, which is good.
CesarGon
2010-10-10 20:47:41