【问题标题】:How to forbid users to close the form? (MS VS C# 2010) [duplicate]如何禁止用户关闭表单? (MS VS C# 2010)[重复]
【发布时间】:2011-08-05 11:20:34
【问题描述】:

可能重复:
How to Disable Alt + F4 closing form?

请帮帮我。

如何制作仅从程序代码关闭的模态表单。 当用户按下 Alt+F4 等时,我想防止表单关闭。

我正在使用 MS VS C# 2010。

【问题讨论】:

  • 从用户的角度来看,让我添加这个离题的建议:如果用户不允许关闭表单,那么至少要确保你的表单没有 [X] 关闭图标,也没有 CloseCancel 按钮或任何其他表明表单可关闭的按钮。如果存在任何这些情况,那么您的表单应该在用户请求时关闭。 (对不起,如果这听起来很傲慢——也许你已经处理了可用性方面的问题。)

标签: c# showdialog topmost


【解决方案1】:

您可以尝试覆盖 formClosing 事件。

只需进入 Visual Studio,选择程序的主窗体,查看事件选项卡中的选项 (f4)。查找 FormClosing 并处理您的新代码。

e.Cancel = true;

^ 把它放在新的代码块中,你会阻止你的申请表在 alt+f4 上关闭

【讨论】:

  • 我试着把回报;陈述。但是没有效果
  • 更新了我的答案。请注意,这是执行此操作的快速而肮脏的方式,您始终可以专门覆盖表单上的 alt+f4 组合。
  • 没问题,请注意,现在您将无法仅使用application.exit(); 关闭您的应用程序,如果您想关闭程序,请先删除事件,然后再关闭。如果您愿意,我会更新答案,但我建议您看这里:stackoverflow.com/questions/14943/…
【解决方案2】:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
   // put your validation here.
   if (validations)
   {
      // Display a MsgBox asking the user to continue  or abort.
      if(MessageBox.Show("message...?", "My Application",
         MessageBoxButtons.YesNo) ==  DialogResult.Yes)
      {
         // Cancel the Closing event from closing the form.
         e.Cancel = true;
      }
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    • 2021-04-15
    相关资源
    最近更新 更多