表单关闭事件处理程序 [英] Form Closing event handler

查看:67
本文介绍了表单关闭事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我有以下代码,其中winform会在关闭表单之前提示我一条消息。当我点击关闭按钮。 YesNo消息将要求我确认。当我单击是,它应该关闭,当不,它应该保持。但问题是无论我点击是或否,winform也会关闭。以下是我的代码。任何帮助将不胜感激。



<前lang =cs> 私人 void Form1_Closing( object sender,System.ComponentModel.CancelEventArgs e)
{
DialogResult confirm = MessageBox。显示( 确认退出应用程序? 确认,MessageBoxButtons.YesNo);

if (confirm == DialogResult.Yes)
{
Application.Exit();
}
}

解决方案

你应该使用 e.Cancel = true; for else branch。


hi i have the following codes in which the winform will prompt me a message before closing the form. When i click the close button. A YesNo message will ask me for confirmation. When i click Yes, it should close and when No, it should remain. But the problem is no matter i click Yes or No, the winform will also close. Below are my codes. Any help would be appreciated.

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    DialogResult confirm = MessageBox.Show("Confirm Exit the application ?", "Confirmation", MessageBoxButtons.YesNo);

    if (confirm == DialogResult.Yes)
    {
        Application.Exit();
    }
}

解决方案

You should use e.Cancel = true; for else branch.


这篇关于表单关闭事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆