日期:2014-05-18  浏览次数:20760 次

|M| 我用按钮来做退出代替窗口的X不成功 程序退出不了 大家看看
点窗口的X按钮是事件 我把他变成了最小化到托盘去
C# code

        #region 托盘程序
        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Visible = false;
            nIcon.Visible = true;
            e.Cancel = true;

        }
        private void nIcon_DoubleClick(object sender, EventArgs e)
        {
            this.Visible = true;
            nIcon.Visible = false;
        }
        #endregion


然后我有按钮
C# code

        private void tMenu05_0_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


但我点击这个迎按钮的时候他就是退不出 应为他又去执行了Main_FormClosing
而Main_FormClosing中有e.Cancel = true;所以退不了

麻烦大家说说这里要怎么改

本来我想把
C# code

this.Visible = false;
nIcon.Visible = true;


写到点击X的那个按钮的但找不到有这个事件

谢谢

------解决方案--------------------
最好不使用Application.Exit()来退现程序,而使用关闭主窗口的方法来关闭:


C# code

        bool exit = false;
        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!exit)
            {
                this.Visible = false;
                nIcon.Visible = true;
                e.Cancel = true;
            }
        }
        private void nIcon_DoubleClick(object sender, EventArgs e)
        {
            this.Visible = true;
            nIcon.Visible = false;
        }
        private void tMenu05_0_Click(object sender, EventArgs e)
        {
            exit = true;
            this.Close();
        }

------解决方案--------------------
在Closing里面有CloseReason来判断~~~~`
------解决方案--------------------
Closing