Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c窗体中向退出按钮添加代码?_C#_Button_Exit - Fatal编程技术网

C# 如何在c窗体中向退出按钮添加代码?

C# 如何在c窗体中向退出按钮添加代码?,c#,button,exit,C#,Button,Exit,我正在使用Microsoft Visual C 2008 Express 在我的主窗体中,右上角有一个关闭窗体的X按钮。如何将代码添加到此按钮?在我的菜单中,我有一个退出项,它有清理和关闭数据库的代码。如果用户选择该按钮作为退出方式,我如何向该按钮添加相同的代码 谢谢 -Adeena使用winform的Closed事件。使用FormClosed事件应捕获关闭表单的任何方式。在表单的设计视图中,在属性窗口中,选择事件按钮并向下滚动到FormClosed和FormClosed事件 /// <s

我正在使用Microsoft Visual C 2008 Express

在我的主窗体中,右上角有一个关闭窗体的X按钮。如何将代码添加到此按钮?在我的菜单中,我有一个退出项,它有清理和关闭数据库的代码。如果用户选择该按钮作为退出方式,我如何向该按钮添加相同的代码

谢谢


-Adeena

使用winform的Closed事件。

使用FormClosed事件应捕获关闭表单的任何方式。

在表单的设计视图中,在属性窗口中,选择事件按钮并向下滚动到FormClosed和FormClosed事件

/// <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);
}
FormClosed在窗体关闭后调用

表单关闭在表单关闭前调用,还允许您取消关闭,保持表单打开:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;
}

FormClosing/FormClosed允许您查看该表单的事件,该事件可能与退出的应用程序一致。但是,您可以连接另一个事件Application.ApplicationExit

在您的主要方法中:

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

}

此代码将捕获用户单击“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);
}

如果您想询问用户是否确实要关闭此表单?,请使用FormClosing,您可以在其中设置Cancel=True,表单将保持打开状态

如果只想在表单确实关闭时关闭某些资源,则使用FormClosed事件


如果您控制着整个代码,那么这就不重要了。但您不希望在事件的另一个处理程序将表单保持打开状态时,使用FormClosing清理资源。

然后双击表单设计中的退出按钮 只需调用Dispose方法

表单操作方法 //


那是FormClose还是FormClosing?或者,如果我所做的只是关闭数据库之类的事情,这无关紧要吗?对不起,输入错误:我指的是关闭的事件。但是我刚刚读到它被.NET2.0Updwards中的FormClosed事件所取代
 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();
}