本文介绍了当另一种表单关闭时,如何以一种形式运行某些代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个名为"form1"的主窗体,从中可以单击一个按钮以显示另一个名为"form2"的窗体.

So I have a main form called ''form1'' from which I can click a button to show another form called ''form2''. How can I run some code in ''form1'' when the event of ''form2'' closing occurs?

推荐答案

form2.FormClosed += (sender, eventArgs) => {
    // some code
};


form2.FormClosing += (sender, eventArgs) => {
    Form senderForm = (Form)sender; // will always cast to this type, no need to check up, see below
    // some code
    // but you can also prevent closing of the form based of some condition:
    if (shouldNotCloseIt /* whatever */ && eventArgs.CloseReason == UserClosing) {
        eventArgs.Cancel = true;
        senderForm.Hide(); // typical use of it; actually senderForm == form2, see above
    } // if
    // some more code
}



请参阅:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosed.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.forms.closereason.aspx [ ^ ].

—SA



Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosed.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.closereason.aspx[^].

—SA


这篇关于当另一种表单关闭时,如何以一种形式运行某些代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:17