2014-06-06 186 views
0

這應該很簡單,但我無法使其工作。我搜索了SO和Google,令人驚訝的是我還沒有找到答案。當另一個表格關閉時,我想要做的就是關閉一個表單。第二種形式是在點擊按鈕時打開。當Form1關閉時,Form2也應該關閉。 Form2可能無法打開,因此我們需要檢查它是否首先打開。這是我一直在努力:當另一個窗體關閉時關閉窗體

Private Sub frm_scu_config_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing 

    ' this isn't working 
    If Application.OpenForms().OfType(Of frm_scu_report_display).Any Then 

     Dim frmConfig As frm_scu_report_display 

     ' Open the config form and pass the list of turbines 
     frmConfig = New frm_scu_report_display() 
     frmConfig.Close() 

    End If 
End Sub 

任何幫助,將不勝感激。

+0

'如果Form2的狀態並沒有沒有那麼Form2.Close' – Plutonix

+0

使用布爾變量作爲標誌.. – matzone

+0

謝謝@Plutonix。如果我在if語句中拋出一個消息框,它會正確識別第二個表單是否打開,但它不會關閉它。 – mack

回答

1

在LINQ查詢中,您已經擁有了更好的一半答案,但是您創建了一個新實例frm_scu_report_display並嘗試關閉此(未打開)實例。
如果我沒弄錯了,應該如果你留下來,你已經有了正確的道路上工作:

While Application.OpenForms().OfType(Of frm_scu_report_display).Any 
     Application.OpenForms().OfType(Of frm_scu_report_display).First.Close() 
    End While 
+0

謝謝@KekuSemau!這工作完美。也感謝在邏輯中解釋我的錯誤。 :) – mack

+1

不要在生產中試用。 –

+0

是的,我也不會使用它,只是說明它如何與他已有的... – KekuSemau