C# 如何禁用 Alt + F4 关闭表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14943/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 08:49:03  来源:igfitidea点击:

How to Disable Alt + F4 closing form?

提问by Ryan Sampson

What is the best way to disable Alt+ F4in a c# win form to prevent the user from closing the form?

在 ac# win 表单中禁用Alt+F4以防止用户关闭表单的最佳方法是什么?

I am using a form as a popup dialog to display a progress bar and I do not want the user to be able to close it.

我使用表单作为弹出对话框来显示进度条,我不希望用户能够关闭它。

采纳答案by Martin

This does the job:

这可以完成以下工作:

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

Edit: In response to pix0rs concern - yes you are correct that you will not be able to programatically close the app. However, you can simply remove the event handler for the form_closing event before closing the form:

编辑:为了回应 pix0rs 的担忧 - 是的,您将无法以编程方式关闭应用程序是正确的。但是,您可以在关闭表单之前简单地删除 form_closure 事件的事件处理程序:

this.FormClosing -= new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Close();

回答by Timbo

You could handle the FormClosingevent and set FormClosingEventArgs.Cancelto true.

您可以处理该FormClosing事件并设置FormClosingEventArgs.Canceltrue.

回答by pix0r

Would FormClosing be called even when you're programatically closing the window? If so, you'd probably want to add some code to allow the form to be closed when you're finished with it (instead of always canceling the operation)

即使您以编程方式关闭窗口,也会调用 FormClosing 吗?如果是这样,您可能需要添加一些代码以允许在完成表单时关闭表单(而不是总是取消操作)

回答by Lasse V. Karlsen

Note that it is considered bad form for an application to completely prevent itself from closing. You should check the event arguments for the Closing event to determine how and why your application was asked to close. If it is because of a Windows shutdown, you should not prevent the close from happening.

请注意,应用程序完全阻止自身关闭被认为是不良形式。您应该检查 Closing 事件的事件参数,以确定您的应用程序被要求关闭的方式和原因。如果是因为 Windows 关闭,则不应阻止关闭发生。

回答by Orion Edwards

I am using a form as a popup dialog to display a progress bar and I do not want the user to be able to close it.

我使用表单作为弹出对话框来显示进度条,我不希望用户能够关闭它。

If the user is determined to close your app (and knowledgeable) enough to press alt+f4, they'll most likely also be knowledgeable enough to run task manager and kill your application instead.

如果用户决定关闭您的应用程序(并且知识渊博)以按 alt+f4,他们很可能也有足够的知识来运行任务管理器并终止您的应用程序。

At least with alt+f4 your app can do a graceful shutdown, rather than just making people kill it. From experience, people killing your app means corrupt config files, broken databases, half-finished tasks that you can't resume, and many other painful things.

至少使用 alt+f4 你的应用程序可以正常关闭,而不是让人们杀死它。根据经验,人们杀死您的应用程序意味着损坏的配置文件、损坏的数据库、无法恢复的未完成的任务以及许多其他痛苦的事情。

At least prompt them with 'are you sure' rather than flat out preventing it.

至少用“你确定吗”来提示他们,而不是完全阻止它。

回答by Matt Warren

If you look at the valueof FormClosingEventArgs e.CloseReason, it will tell you why the form is being closed. You can then decide what to do, the possible values are:

如果你看一下价值FormClosingEventArgs e.CloseReason,它会告诉你为什么形式正在被关闭。然后你可以决定做什么,可能的值是:

Member name- Description

会员名称- 描述



None- The cause of the closure was not defined or could not be determined.

- 关闭的原因未定义或无法确定。

WindowsShutDown- The operating system is closing all applications before shutting down.

WindowsShutDown- 操作系统在关闭前关闭所有应用程序。

MdiFormClosing- The parent form of this multiple document interface (MDI) form is closing.

MdiFormClosing- 此多文档界面 (MDI) 表单的父表单正在关闭。

UserClosing- The user is closing the form through the user interface (UI), for example by clicking the Close button on the form window, selecting Close from the window's control menu, or pressing ALT+F4.

UserClosing- 用户通过用户界面 (UI) 关闭表单,例如通过单击表单窗口上的关闭按钮、从窗口的控制菜单中选择关闭或按ALT+ F4

TaskManagerClosing- The Microsoft Windows Task Manager is closing the application.

TaskManagerClosing- Microsoft Windows 任务管理器正在关闭应用程序。

FormOwnerClosing- The owner form is closing.

FormOwnerClosing- 所有者表单正在关闭。

ApplicationExitCall- The Exit method of the Application class was invoked.

ApplicationExitCall- 调用了 Application 类的 Exit 方法。

回答by antsyawn

I believe this is the right way to do it:

我相信这是正确的方法:

protected override void OnFormClosing(FormClosingEventArgs e)
{
  switch (e.CloseReason)
  {
    case CloseReason.UserClosing:
      e.Cancel = true;
      break;
  }

  base.OnFormClosing(e);
}

回答by linquize

Subscribe FormClosing event

订阅 FormClosing 事件

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

Only one line in the method body.

方法体中只有一行。

回答by Bharath theorare

Hide close button on form by using the following in constructor of the form:

通过在表单的构造函数中使用以下内容隐藏表单上的关闭按钮:

this.ControlBox = false;

回答by Brahim Bourass

This is a hack to disable Alt+ F4.

这是禁用Alt+的技巧F4

private void test_FormClosing(object sender, FormClosingEventArgs e)
{
    if (this.ModifierKeys == Keys.Alt || this.ModifierKeys == Keys.F4) 
    { 
        e.Cancel = true; 
    }    
}