有条件的代表问题

soo*_*ise 2 c# winforms

假设我打开一个表单,并希望在它关闭后附加一个命令.

FormZombie FormZombie = new FormZombie();
FormZombie.Show();
FormZombie.FormClose += delegate{Utilities.DoSomethingCool()};
Run Code Online (Sandbox Code Playgroud)

我怎样才能使Utilities.DoSomethingCool()触发器只执行取决于FormZombie中发生的事情?

Ree*_*sey 8

您可以将条件检查添加到您的委托中:

FormZombie formZombie = new FormZombie(); 
formZombie.Show(); 
formZombie.FormClose += 
    delegate
    {
        if (formZombie.AteEnoughBrains)
            Utilities.DoSomethingCool();
    };
Run Code Online (Sandbox Code Playgroud)

  • +1指的是吃大脑. (2认同)