C# FormClosing 和 FormClosed 事件不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19641427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FormClosing and FormClosed events do not work
提问by Jean
I'm developing a C#
app
And i need do some validations before the user close the form.
我正在开发一个C#
应用程序,我需要在用户关闭表单之前进行一些验证。
I tried to use the FormClosing
event, but it didn't work,
later I used the FormClosed
event, but the same.
我尝试使用该FormClosing
事件,但没有奏效,后来我使用了该FormClosed
事件,但还是一样。
The problem is, when I click in the "close button" (on the top of the form) it doesn't do anything but i have the events in form properties and everything.
问题是,当我单击“关闭按钮”(在表单顶部)时,它什么也没做,但我在表单属性和所有内容中都有事件。
this is my code:
这是我的代码:
private void Inicio_FormClosing_1(object sender, FormClosingEventArgs e)
{
//things I have to do
//...
//...
if(bandera==true)
Application.Exit();
}
and
和
private void Inicio_FormClosed_1(object sender, FormClosingEventArgs e)
{
//things I have to do
//...
//...
if(bandera==true)
Application.Exit();
}
any idea?
任何的想法?
Thank you
谢谢
采纳答案by Jean
I found the mistake;
我发现了错误;
Here: (when I initialize my form)
这里:(当我初始化我的表单时)
public Inicio()
{
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(635, 332);
this.StartPosition = FormStartPosition.CenterScreen;
llenaForm(nombreFormulario);
Application.EnableVisualStyles();
}
All i needed was: InitializeComponent();
I deleted by mistake
我只需要: InitializeComponent();
我误删了
It should be:
它应该是:
public Inicio()
{
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll = true;`
InitializeComponent();//<<<<<<<<-------------------
this.ClientSize = new System.Drawing.Size(635, 332);
this.StartPosition = FormStartPosition.CenterScreen;
llenaForm(nombreFormulario);
Application.EnableVisualStyles();
}
Thank you so much guys!
十分感谢大家!
回答by varocarbas
Both events should work fine. Just open a new project and do this simple test:
这两个事件都应该正常工作。只需打开一个新项目并进行这个简单的测试:
private void Form1_Load(object sender, EventArgs e)
{
this.FormClosing += new FormClosingEventHandler(Inicio_FormClosing_1);
this.FormClosed += new FormClosedEventHandler(Inicio_FormClosed_1);
}
private void Inicio_FormClosing_1(object sender, FormClosingEventArgs e)
{
//Things while closing
}
private void Inicio_FormClosed_1(object sender, FormClosedEventArgs e)
{
//Things when closed
}
If you set break points in these methods, you would see that they are reached after the close button is clicked. It seems that there is some problem in your event-attaching code. For example: Inicio_FormClosed_1(object sender, FormClosingEventArgs e)
is wrong, as far as it should take a FormClosedEventArgs
argument; and thus this method is surely not associated with the FormClosed event
(otherwise, the code wouldn't compile).
如果在这些方法中设置断点,您会看到在单击关闭按钮后到达它们。您的事件附加代码中似乎存在一些问题。例如:Inicio_FormClosed_1(object sender, FormClosingEventArgs e)
是错误的,只要它需要一个FormClosedEventArgs
论点;因此,此方法肯定与 无关FormClosed event
(否则,代码将无法编译)。
回答by Brian Rogers
In order to prevent the user from closing a form in response to certain validations, you need to set FormClosingEventArgs.Cancel = true
.
为了防止用户关闭表单以响应某些验证,您需要设置FormClosingEventArgs.Cancel = true
.
For example:
例如:
private void Inicio_FormClosing_1(object sender, FormClosingEventArgs e)
{
if (string.IsNullOrEmpty(txtSomethingRequired.Text))
{
MessageBox.Show("Something is required here!");
if (txtSomethingRequired.CanFocus) txtSomethingRequired.Focus();
e.Cancel = true;
return;
}
}
You can only do validations in the FormClosing
event to prevent the form from closing; if you wait until FormClosed
it is already too late.
您只能在FormClosing
事件中进行验证以防止表单关闭;如果你等到FormClosed
已经太晚了。
回答by L_7337
I noticed you have an "_1" at the end of your method names. Did you rename these methods?
我注意到您的方法名称末尾有一个“_1”。你重命名了这些方法吗?
If so, your UI code(designer file) will need to be updated with these new method names.
如果是这样,您的 UI 代码(设计器文件)将需要使用这些新方法名称进行更新。
You can put a breakpoint in these methods to see if they are getting called.
您可以在这些方法中放置一个断点以查看它们是否被调用。
回答by Jamisco
Just as a side note, the Form.Hide() method does not raised the form_closed or form_closing events
作为旁注,Form.Hide() 方法不会引发 form_closed 或 form_closed 事件