在 C# 中关闭 Window 表单应用程序中的 window 时发出警报
Alert on Closing window in Window Form Application in C#
我希望当用户通过单击 (X) 或按 ESC 键或按 [=4 关闭(窗口窗体应用程序,C#)中的 window 时=]ALT+F4,将显示一个警告,即一个对话框(包含两个按钮 OK 和 CANCEL)。
怎么办?
您可以通过简单的搜索找到它!
处理您表单的 Closing
事件:
this.Closing += OnClosing; // For example put this in the constructor of your form
private void OnClosing(object sender, CancelEventArgs cancelEventArgs)
{
string msg = "Do you want to close this?";
DialogResult result = MessageBox.Show(msg, "Close Confirmation",
MessageBoxButtons.YesNo/*Cancel*/, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
/* Do something */;
else if (result == DialogResult.No)
cancelEventArgs.Cancel = false;
}
您可以在应用程序级别执行此操作
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ApplicationExit += Application_ApplicationExit;
AppDomain.CurrentDomain.ProcessExit += Application_ApplicationExit;
Application.Run(new Form1());
}
static void Application_ApplicationExit(object sender, EventArgs e)
{
//Do something
}
我希望当用户通过单击 (X) 或按 ESC 键或按 [=4 关闭(窗口窗体应用程序,C#)中的 window 时=]ALT+F4,将显示一个警告,即一个对话框(包含两个按钮 OK 和 CANCEL)。 怎么办?
您可以通过简单的搜索找到它!
处理您表单的 Closing
事件:
this.Closing += OnClosing; // For example put this in the constructor of your form
private void OnClosing(object sender, CancelEventArgs cancelEventArgs)
{
string msg = "Do you want to close this?";
DialogResult result = MessageBox.Show(msg, "Close Confirmation",
MessageBoxButtons.YesNo/*Cancel*/, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
/* Do something */;
else if (result == DialogResult.No)
cancelEventArgs.Cancel = false;
}
您可以在应用程序级别执行此操作
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ApplicationExit += Application_ApplicationExit;
AppDomain.CurrentDomain.ProcessExit += Application_ApplicationExit;
Application.Run(new Form1());
}
static void Application_ApplicationExit(object sender, EventArgs e)
{
//Do something
}