Mar*_*tin 103
这样做的工作:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
Run Code Online (Sandbox Code Playgroud)
编辑:回应pix0rs关注 - 是的,你是正确的,你将无法以编程方式关闭应用程序.但是,您可以在关闭表单之前删除form_closing事件的事件处理程序:
this.FormClosing -= new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Close();
Run Code Online (Sandbox Code Playgroud)
Mat*_*ren 57
如果你看一下价值的FormClosingEventArgs e.CloseReason,它会告诉你为什么形式正在被关闭.然后,您可以决定要做什么,可能的值是:
会员名称 - 说明
无 - 关闭的原因未定义或无法确定.
WindowsShutDown - 操作系统在关闭之前关闭所有应用程序.
MdiFormClosing - 此多文档界面(MDI)表单的父表单正在关闭.
UserClosing - 用户通过用户界面(UI)关闭表单,例如单击窗体窗口上的"关闭"按钮,从窗口的控制菜单中选择"关闭",或按ALT+ F4.
TaskManagerClosing - Microsoft Windows任务管理器正在关闭该应用程序.
FormOwnerClosing - 所有者表单正在关闭.
ApplicationExitCall - 调用Application类的Exit方法.
ant*_*awn 25
我相信这是正确的方法:
protected override void OnFormClosing(FormClosingEventArgs e)
{
switch (e.CloseReason)
{
case CloseReason.UserClosing:
e.Cancel = true;
break;
}
base.OnFormClosing(e);
}
Run Code Online (Sandbox Code Playgroud)
ang*_*son 16
请注意,应用程序被认为是完全阻止自身关闭的错误形式.您应该检查Closing事件的事件参数,以确定要求关闭应用程序的方式和原因.如果是因为Windows关闭,则不应阻止关闭发生.
Ori*_*rds 12
我使用窗体作为弹出对话框来显示进度条,我不希望用户能够关闭它.
如果用户决定关闭你的应用程序(并且知识渊博)足以按下alt + f4,他们很可能也足够知识来运行任务管理器并杀死你的应用程序.
至少使用alt + f4你的应用程序可以正常关闭,而不仅仅是让人们杀死它.根据经验,杀死应用程序的人意味着配置文件损坏,数据库损坏,无法恢复的半完成任务以及许多其他痛苦的事情.
至少提示他们'你确定',而不是防止它.
小智 5
这是禁用Alt+ 的技巧F4。
private void test_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.ModifierKeys == Keys.Alt || this.ModifierKeys == Keys.F4)
{
e.Cancel = true;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
65930 次 |
| 最近记录: |