tags:

views:

33

answers:

1

Hi SO,

Let's say I open a form and want to attach a command to it after it closes.

FormZombie FormZombie = new FormZombie();
FormZombie.Show();
FormZombie.FormClose += delegate{Utilities.DoSomethingCool()};

How can I make Utilities.DoSomethingCool() trigger only executes depending on what happens in FormZombie?

+8  A: 

You can add the conditional check into your delegate:

FormZombie formZombie = new FormZombie(); 
formZombie.Show(); 
formZombie.FormClose += 
    delegate
    {
        if (formZombie.AteEnoughBrains)
            Utilities.DoSomethingCool();
    };
Reed Copsey
+1 for referring to the eating of brains.
Brian Gideon
Reed, how can I implement this if I'm two forms deep? Form1 opens Form2, Form2 opens Form3, Form3.Closed += delegate{method from Form1}
Soo
You should to pass a reference into the forms - ie: Give Form1 a reference to your form1 instance (this), then Form2 can pass the form1 instance into form3, etc. That way, it can handle this directly.
Reed Copsey
@Reed, as always, your solution worked perfectly. Thanks much!
Soo