如何将代码添加到 ac# 形式的退出按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/904345/
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
How do I add code to the exit button in a c# form?
提问by adeena
I'm using Microsoft Visual C# 2008 Express.
我正在使用 Microsoft Visual C# 2008 Express。
In my main form, there's that X button to close the form at the top right. How do I add code to this button? In my menu, I have an "Exit" item and it has code that cleans up and closes my databases. How do I add the same code to this button if the user chooses that as a way to exit?
在我的主窗体中,右上角有一个 X 按钮可以关闭窗体。如何向此按钮添加代码?在我的菜单中,我有一个“退出”项,它有清理和关闭我的数据库的代码。如果用户选择将相同的代码作为退出方式,我该如何向该按钮添加相同的代码?
Thanks!
谢谢!
-Adeena
-阿迪娜
采纳答案by Peter C
Using the FormClosing Event should catch any way of closing the form.
使用 FormClosing 事件应该可以捕获关闭表单的任何方式。
回答by Stephan Keller
Use the Closed-Event of your winform.
使用您的 winform 的 Closed-Event。
回答by Matt Brindley
In the Design view for your form, within the Properties window, select the Events button and scroll down to the "FormClosed" and "FormClosing" events.
在窗体的设计视图中,在属性窗口中,选择事件按钮并向下滚动到“FormClosed”和“FormClosing”事件。
FormClosed is called after the form is closed.
FormClosed 在表单关闭后被调用。
FormClosing is called before the form is closed and also allows you to cancel the close, keeping the form open:
FormClosing 在表单关闭之前调用,还允许您取消关闭,保持表单打开:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
回答by Josh
FormClosing/FormClosed lets you watch the event for that form which may coincide with the application exiting. However, there is another event you can wire up called Application.ApplicationExit.
FormClosing/FormClosed 允许您观察该表单的事件,该事件可能与应用程序退出一致。但是,您可以连接另一个名为 Application.ApplicationExit 的事件。
In your Main method:
在您的 Main 方法中:
Application.ApplicationExit += Application_ApplicationExit;
...
private static void Application_ApplicationExit(object sender, EventArgs e) {
// do stuff when the application is truly exiting, regardless of the reason
}
回答by uzbones
This code will capture the user clicking on the 'X' or using Alt-F4 on a form to allow you to do something. I had to use this because the I needed the action to call my closing event, and it wouldn't call it when using the FormClosing Event due to racing events.
此代码将捕获用户单击“X”或在表单上使用 Alt-F4 以允许您执行某些操作。我必须使用它,因为我需要该操作来调用我的关闭事件,并且由于赛车事件而在使用 FormClosing 事件时它不会调用它。
/// <summary>
/// This code captures the 'Alt-F4' and the click to the 'X' on the ControlBox
/// and forces it to call MyClose() instead of Application.Exit() as it would have.
/// This fixes issues where the threads will stay alive after the application exits.
/// </summary>
public const int SC_CLOSE = 0xF060;
public const int WM_SYSCOMMAND = 0x0112;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
MyClose();
base.WndProc(ref m);
}
回答by van
If you want to ask the user "Are you sure you want do close this form?", then use FormClosing
, where you can set Cancel = True
and the form would remain open.
如果您想询问用户“您确定要关闭此表单吗?”,请使用FormClosing
,您可以在其中进行设置Cancel = True
并且表单将保持打开状态。
If you want to close some resource only when the form is definitely closed, then you use FormClosed
event.
如果您只想在表单肯定关闭时关闭某些资源,那么您可以使用FormClosed
事件。
If you are in control of the whole code, then it kind of does not matter. But what you do not want to happen is to clean-up the resources using FormClosing
when the the other handler of the event will keep the form open.
如果您可以控制整个代码,那么这无关紧要。但是您不希望发生的是FormClosing
在事件的另一个处理程序将保持表单打开时使用的清理资源。
回答by Santosh Thakur
double click the exit button in form'design then simply call the Dispose() method
双击 form'design 中的退出按钮,然后调用 Dispose() 方法
回答by Sumon Banerjee
Form action method //
表单动作方法 //
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
if (e.CloseReason == CloseReason.WindowsShutDown) return;
switch (MessageBox.Show(this, "Are you sure you want to exit?", "Exit", MessageBoxButtons.YesNo))
{
case DialogResult.No:
e.Cancel = true;
break;
default:
break;
}
}
回答by Adumuah Dowuona
You can use form closing events choose or set closing event in properties window
You can add dialog conditions based on what task you want to perform before closing form
private void Form1_FormClosing(object sender,FormClosingEventArgs e)
{
Application.Exit();
//you can also use Application.ExitThread();
}