我想创建一个方法,当按下关闭应用程序的x按钮时(它在右上角)我将所有文本保存到文件中。从我有限的c#体验中,当我点击一个按钮或文本框时,会创建一个方法,但是当我点击x时,它会将我带到加载Windows窗体的方法。当我进行谷歌搜索时,结果指出我是否要关闭应用程序,而不是在表单关闭时发生事件。如何在表单关闭时创建事件?
答案 0 :(得分:1)
如果您在Visual Studio的Designer中选择了表单,则可以从“属性”窗口中选择FormClosed事件,它将自动生成表单关闭时的事件方法,然后您可以将相关代码添加到
答案 1 :(得分:1)
您可以注册Form.FormClosing
事件:
Form form1 = new Form();
form1.FormClosing += (o, e) =>
{
// Do stuff you want
}
答案 2 :(得分:1)
在表单加载上添加此代码:
this.FormClosing += MainPage_FormClosing; // occurs before closing of the form
this.FormClosed += MainPage_FormClosed; // occurs after the closing of the form
private void MainPage_FormClosed(object sender, FormClosedEventArgs e)
{
// your code here to do something before closing the form
}
private void MainPage_FormClosing(object sender, FormClosingEventArgs e)
{
// your code here to do something after the form is closed
}
我认为这是你问题的答案。