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

Solution 1:

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";
    }


}

Solution 2:

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.