tags:

views:

422

answers:

2

I am looking to refresh form1 when form 2 is closed. I know to use a Closing Event from form2, but thats where I get lost.

Thanks

+2  A: 

One solution is to pass a reference of Form1 to Form2 in the constructor and just call f1.Invalidate(true) on the closing event of Form2.

Nazgulled
I’m not opposed to using this as a solution either, but as a beginner I’ve never used a constructor before. So I'm not sure what you mean.
JimDel
+3  A: 

A good way to achieve this is to use the Mediator pattern. In this way your forms don't have to necessarily know about each other. Allow the Mediator to manage the interaction between the forms so each individual form can concentrate on its own responsibilities.

A very crude Mediator that would achieve what you want could be implemented like so:

public class FormMediator
{
    public Form MainForm { private get; set; }
    public Form SubForm { private get; set; }

    public void InitializeMediator()
    {
     MainForm.FormClosed += MainForm_FormClosed;
    }

    void MainForm_FormClosed(object sender, FormClosedEventArgs e)
    {
     SubForm.Refresh();
    }
}

Now your sub form will update whenever the main form is closed, and neither has to know anything about each other.

EDIT:

Ok, so I am going to put this solution out that will get you started, but please note that this is only a rudimentary implementation of a Mediator pattern. I would highly encourage you to read up about that pattern, and design patterns in general in order to gain a better understanding of what is going on.

Again, this is a sample, but it does have some basic error checking and should get you going.

Your form declaration is going to look something like this:

public partial class MainForm : Form
{
    private FormMediator _formMediator;

    public MainForm()
    {
     InitializeComponent();
    }

    public void SomeMethodThatOpensTheSubForm()
    {
     SubForm subForm = new SubForm();

     _formMediator = new FormMediator(this, subForm);

     subForm.Show(this);
    }
}

And the modified implementation of the Mediator would look like this:

public class FormMediator
{
    private Form _subForm;
    private Form _mainForm;

    public FormMediator(Form mainForm, Form subForm)
    {
     if (mainForm == null)
      throw new ArgumentNullException("mainForm");

     if (subForm == null)
      throw new ArgumentNullException("subForm");

     _mainForm = mainForm;
     _subForm = subForm;

     _subForm.FormClosed += MainForm_FormClosed;
    }

    void MainForm_FormClosed(object sender, FormClosedEventArgs e)
    {
     try
     {
      _mainForm.Refresh();
     }
     catch(NullReferenceException ex)
     {
      throw new InvalidOperationException("Unable to close the Main Form because the FormMediator no longer has a reference to it.", ex);
     }
    }
}
Josh
It's actually a Design Pattern. So the class didn't exist until Josh wrote it. :) As to how you would use it, your application would create an instance of it and set its MainForm and SubForm properties.
GalacticCowboy
FWIW, you could actually call the forms "SubscriberForm" and "SubscribedForm" if that makes it clearer. Subscriber is the one you want to refresh, and Subscribed is the one whose Close event you want to grab.
GalacticCowboy
That's awesome Josh. Plugged it all in and it works as advertised! I Learned something new tonight. Thanks!
JimDel
Like I said, this is good for now, but I would highly encourage you to read up on some programming design books. Code Complete is a good place to start and it will open your eyes to a whole new world of programming.
Josh