解决winform 关闭多次提示

在winform窗体点击某个按钮 实现退出程序   在网上看到到的是要用

Application.Exit();

 于是当时用了:

        private void bntEsce_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否要退出系统!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

 这下点击按钮是实现退出程序了.但是又想在点击窗口右上角的"关闭"同时也要提示用户,又定义了窗口的closeing()事件:

 1         private void frm_FormClosing(object sender, FormClosingEventArgs e)
 2         {
 3             if (MessageBox.Show("是否要退出系统!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
 4             {
 5                 this.Close();
 6             }
 7             else
 8             {
 9                 e.Cancel = true;
10             }
11         }

 

 调试的时候又出现问题了,点击窗体关闭按钮的时候,一直会提示是否关闭.最后百度才知道原来

this.Close();

 会响应窗体 closeing()事件,才导致了一直提示 .把closeing()事件中的 this.close();这句去掉.调试运行这下不会一直提示了,但是点击自定义的按钮退出窗体还是会提示两次.

头都大了,继续琢磨.查资料 原来自定义退出按钮事件中的

Application.Exit();

 也会响应窗体closeing()事件.那就在自定义按钮这个事件中不在提示了.最后得到最终解决方法^_^

 

 1         /// <summary>
 2         /// "窗口关闭"
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void DaiGe_FormClosing(object sender, FormClosingEventArgs e)
 7         {
 8             if (MessageBox.Show("是否要退出系统!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
 9             {
10                 //base.Dispose();写上这句可以实现退出,什么也不写也可以.
11             }
12             else
13             {
14                 e.Cancel = true;
15             }
16         }
17         /// <summary>
18         /// "btn退出"
19         /// </summary>
20         /// <param name="sender"></param>
21         /// <param name="e"></param>
22         private void bntEsce_Click(object sender, EventArgs e)
23         {
24                 Application.Exit();
25         }

没办法,我这个菜鸟只能瞎琢磨,不过 终于解决了.一个早上没白费,哈哈

 

posted @ 2014-02-20 23:46  老木瓜  阅读(670)  评论(0)    收藏  举报