在代码中添加表单关闭事件到特定对象实例?

问题描述 投票:3回答:4

非常简单我想做什么,只是希望能够在表单关闭后运行一些代码。

Form1 f = new Form1();
f.Show();
f.formClosing ... <- I just want to run code from this context once this form has been closed
c# events
4个回答
10
投票
{
    Form1 f = new Form1();
    f.FormClosed += new FormClosedEventHandler(f_FormClosed);
    f.Show();
}

void f_FormClosed(object sender, FormClosedEventArgs e)
{
    // Do stuff here
}

9
投票

你可以处理Form.FormClosing事件。

this.FormClosing += new FormClosingEventHandler(myForm_FormClosing);

void myForm_FormClosing(object sender, FormClosingEventArgs e)
{
    //your code here
}

0
投票

Form.FormClosing发生在表格关闭之前。如果您取消此活动,表单将保持打开状态。

要处理的正确事件是Form.FormClosed

form.FormClosed += 
     new Form.FormClosedEventHandler( Place the name of the handler method here ... );

0
投票

现代内联:

FormClosed += (s, a) => { /* your code here */ };

推荐问答