How do you pass an object from form1 to form2 and back to form1?

What you need to do is create a second constructor to your second form that accepts an object as a parameter... for all I care, it could be the entire Form1 object instance, then you can get whatever you want from it. Preserve this object in your second form and modify it as needed there. Upon completion of your second form, your first form will have that data and you can do whatever "refreshing" once the second form closes.

What you need to do is create a second constructor to your second form that accepts an object as a parameter... for all I care, it could be the entire Form1 object instance, then you can get whatever you want from it. Preserve this object in your second form and modify it as needed there. Upon completion of your second form, your first form will have that data and you can do whatever "refreshing" once the second form closes.

Public partial class YourSecondForm : Form { object PreserveFromFirstForm; public YourSecondForm() { ... its default Constructor... } public YourSecondForm( object ParmFromFirstForm ) : this() { this. PreserveFromFirstForm = ParmFromFirstForm; } private void YourSecondFormMethodToManipulate() { // you would obviously have to type-cast the object as needed // but could manipulate whatever you needed for the duration of the second form. This.

PreserveFromFirstForm. Whatever = "something"; } }.

To add to this: from the first form, you would create an instance of the second form and then show it using mySecondForm.ShowDialog(). Since you're passing the parameter object by reference, you can change it however you like in the second form, and those changes will remain in the object when the ShowDialog() call returns. – MusiGenesis Feb 3 '11 at 15:21 @MusiGenesis .. Good point, I completely missed the CALLing to the second form as DIALOG based.

Thanks for the catch. – DRapp Feb 3 '11 at 15:29 Wow. That simple.

Your example and explanation were right on. Thank you. – Noble Bell Feb 3 '11 at 15:56.

I've always liked the eventing model for this. This way your forms don't need to know about anyone else. You can setup an event like the following in some kind of EventHandler class that is used by both forms.

Public delegate void SavingObjectHandler(MyObject obj); public event SavingObjectHandler SavingObjectEvent; public void SavingObject(MyObject obj) { if (SavingObjectEvent! = null) SavingObjectEvent(obj); } then your one form can call the SavingObject even handler and the other can subscribe to the SavingObjectEvent. When the first form triggers the event the second form will be notified do the processing that it needs and the object will then be available to the first form again after the manipulation.

Something like this where the ObservableForm is a base class and contains the ChangeEvent for further flexability: public class FormMain : Form { private ObServableForm childForm = null; public FormMain () { this. ChildForm = new ObservableFormConcreateA(this); this.childForm. ChangeEvent += (sender, e) => Application.DoEvents(); } public void Present() { this.childForm.Show(); } } public class ObservableFormConcreateA ObServableForm { private Form workItemForm = null; private delegate void FormChangedHandler(object source, EventArgs args); //ToDo: this should go in the superclass public event FormChangedHandler ChangeEvent; public FormChild(ObServableFormworkItem) { this.

WorkItemForm = workItem; } public void OnUserActionHandler(object sender, EventArgs e) { this.formItemForm. Property = this. TxtBoxWhateverValue.

Text; if(ChangeEvent! = null) ChangeEvent(this, //create your args to specify which control/data changes); } }.

Wow, I've never seen Application.DoEvents() in a LINQ statement before. :) – MusiGenesis Feb 3 '11 at 16:12.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions