如何判斷是否是主窗體關閉?

 我分別編寫了主窗題和子窗體的Closing事件,可當關閉主窗體時,同時也觸發了子窗體的Closing事件。這個讓我很頭疼。
不過我還是找到了解決方法。即在子窗體中加入一個 ”e.CloseReason“ 的事件判斷。
        //子窗體Closing事件
        mFMEntity.FormClosing += new FormClosingEventHandler(mFMEntity_FormClosing);

        
/// <summary>
        
///關閉時,提示自動保存事件
        
/// scott 20070828
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        static void mFMEntity_FormClosing(object sender, FormClosingEventArgs e)
        {
            //判斷是否是主窗體關閉
           注意:下邊判斷是否是主窗體關閉
            if (e.CloseReason != CloseReason.MdiFormClosing)
            {
                ArrayList alChanged 
= new ArrayList();
                
if (mfrmMain.MdiChildren.Length > 0)
                {
                    
//判斷當前窗體是否是FMEntity
                    if (mfrmMain.ActiveMdiChild is WinControls.FMEntity)
                    {
                        WinControls.FMEntity fmChanged 
= (WinControls.FMEntity)mfrmMain.ActiveMdiChild;
                        
if (fmChanged.Mode == 2)
                        {
                            
//將窗體放入ArrayList中,便於傳遞
                            alChanged.Add(fmChanged);
                        }
                    }
                }
                
if (alChanged != null && alChanged.Count > 0)
                {
                    
int iResult = FrontHelper.ShowExitSaveForm(alChanged);
                    
switch (iResult)
                    {
                        
case 1:
                            
//save
                            foreach (WinControls.FMEntity fmentity in alChanged.ToArray(typeof(WinControls.FMEntity)))
                            {
                                
//保存資料
                                FrontHelper.SaveData();
                            }
                            
break;
                        
case -1:
                            
//don't save
                            break;
                        
case 0:
                            
//cancel
                            e.Cancel = true;
                            
break;
                        
default:
                            
//save
                            break;
                    }
                }
            }  
        }


CloseReason 列舉型別
成員名稱 說明
ApplicationExitCall 已叫用 (Invoke) Application 類別的 Exit 方法。 

FormOwnerClosing 正在關閉主控表單。

MdiFormClosing 正在關閉這個多重文件介面 (MDI) 表單的父表單。  
None 關閉的原因尚未定義或無法判斷。

TaskManagerClosing Microsoft Windows 工作管理員正在關閉應用程式。

UserClosing 使用者正透過使用者介面 (UI) 關閉表單,例如按下表單視窗上的 [關閉] 按鈕、選取視窗控制功能表中的 [關閉] 或按下 ALT+F4。

WindowsShutDown 關機之前,作業系統正在關閉所有應用程式。



        /// <summary>
        /// 主窗體Closing事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private
 void FMMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            
if (this.MdiChildren.Length > 0)
            {
                ArrayList alChanged 
= new ArrayList();
                
for (int iForm = 0; iForm < this.MdiChildren.Length; iForm++)
                {
                    
if (this.MdiChildren[iForm] is WinControls.FMEntity)
                    {
                        WinControls.FMEntity fmChanged 
= (WinControls.FMEntity)this.MdiChildren[iForm];
                        
if (fmChanged.Mode == 2)
                        {
                            alChanged.Add(fmChanged);
                        }
                    }
                }
                
if (alChanged != null && alChanged.Count > 0)
                {
                    
int iResult = FrontHelper.ShowExitSaveForm(alChanged);
                    
switch (iResult)
                    {
                        
case 1:
                            
//save
                            foreach (WinControls.FMEntity fmentity in alChanged.ToArray(typeof(WinControls.FMEntity)))
                            {
                                FrontHelper.SaveData();
                            }
                            
break;
                        
case -1:
                            
//don't save
                            break;
                        
case 0:
                            
//cancel
                            e.Cancel = true;
                            
break;
                        
default:
                            
//save
                            break;
                    }
                } 
            }
        }
posted @ 2008-07-28 12:33  浪子の无悔  阅读(381)  评论(0编辑  收藏  举报