2013-07-19 45 views
1

我在開發WindowsFormApplication,其中一個主窗體存在幾個子窗體。 我的一個表單生成報告,之後我想通過關閉作爲報告生成一部分調用的所有其他中間表單來調用父窗體。當用戶嘗試使用常用關閉按鈕「X」(用於關閉Windows操作系統中的公共窗口的按鈕)關閉報告時,我想調用父窗口。報告生成後調用父窗口

如何隨時從我的任何子窗體訪問我的父窗體/ startupform? 當Last form終止時,如何關閉除主窗體之外的所有其他子窗體?

有人請幫助我.. 在此先感謝..

+0

你如何顯示你的'最終報告'表單?我想知道你如何組織你的表格。例如,我通常在我的主表單的類中定義了我的子/子表單成員。所以我可以很容易地在主窗體中顯示任何子/子窗體。 –

+0

我創建了每個表單,並在需要時通過在特定表單上創建一個對象來調用它們,如下所示:在第一個表單中.. secondform fr = new secondform(); fr.show(); – rtvalluri

回答

0

在WinForms應用程序的應用對象有一個OpenForms集合,其中包含所有打開的表單的引用。您可以從該集合中獲取表單的引用,並在其上調用Close()來關閉它。

而且您的意見,在finalreport的OnClosing事件中,你可以運行此代碼

foreach (Form f in Application.OpenForms) 
      { 
       if (f.GetType().ToString().Contains("start")) 
        f.Focus(); 
      } 
+0

請建議我一些代碼..我的啓動形式名稱是「開始」和最終的形式名稱是「finalreport」..我想調用我的啓動形式時,我關閉窗體「finalreport」 – rtvalluri

+0

我試過了..但啓動窗體不會被調用時,我關閉最終形式:( – rtvalluri

1

要關閉您的最終形式,當顯示啓動窗體,您可以添加事件處理程序,最終的事件FormClosed形式是這樣的:

FinalForm f = new FinalForm(); 
f.FormClosed += (s,e) => { 
    StartupForm sf = new StartupForm(); 
    sf.Show; 
    //if your StartupForm is defined somewhere 
    //just call sf.Show(); 
}; 
//If you are using VS 2005 or below, you have to define a method for FormClosed event handler (unable to use the lambda expression above 
private void FormClosedHandler(object sender, FormClosedEventArgs e){ 
    StartupForm sf = new StartupForm(); 
    sf.Show; 
    //if your StartupForm is defined somewhere 
    //just call sf.Show(); 
} 
//Register the FormClosed event with the event handler above 
f.FormClosed += new FormClosedEventHandler(FormClosedHandler); 

//show your final form 
f.Show(); 
//if this form is closed, the event FormClosed will be raised and the corresponding event handler (we added above) will be called. 
+0

什麼是s和e在此代碼中..我得到錯誤'e'未申報 – rtvalluri

+0

@RavitejA我想你正在使用VS 2005或以下,不支持'Lambda表達式'我寫的是'lambda表達式' –

+0

我使用vs2008 ...當你最終形式的關閉發生時,你能詳細說明事件處理程序嗎? – rtvalluri