2011-10-19 207 views
0

當您點擊「X」按鈕並讓父窗體知道它已關閉時,您如何檢查MDI子窗體的關閉事件?MDI子窗體C#

回答

4

你可以簡單地聽在MDI的FormClosed事件。

var childForm = new ChildForm(); 
childForm.FormClosed += new FormClosedEventHandler(form_FormClosed); 
childForm.Show(); 
0

在窗體的FormClosing事件中,你可以做

TheMainForm form = (TheMainForm)this.MdiParent() 
form.AlertMe(this); 
+0

C#中沒有.AlertMe()方法,我無法找到MDiParent方法,只有一個屬性。 – NexAddo

+0

你會創建你自己的,公開的 –

0

從MainForm的內連接一個封閉的事件給childform

Form mdiChild = new Form(); 
mdiChild.MdiParent = this; 
mdiChild.Closed += (s, e) => { //... }; 
mdiChild.Show(); 

沒有檢查代碼,但不應該是很難

0

好了,下面的代碼演示瞭如何父窗體確認子表單是否已關閉,並且還可以識別是否有任何新的子表單添加到該父表單中。

private List<Form> ChildFormList = new List<Form>(); 

private void MyForm_MdiChildActivate(object sender, EventArgs e) 
{ 
    Form f = this.ActiveMdiChild; 

    if (f == null) 
    { 
    //the last child form was just closed 
    return; 
    } 

    if (!ChildFormList.Contains(f)) 
    { 
     //a new child form was created 
     ChildFormList.Add(f); 
     f.FormClosed += new FormClosedEventHandler(ChildFormClosed); // here the parent form knows that that child form has been closed or not 
    } 
    else 
    { 
    //activated existing form 
    } 
} 
private void ChildFormClosed(object sender, FormClosedEventArgs e) 
{ 
    //a child form was closed 
    Form f = (Form)sender; 
    ChildFormList.Remove(f); 
}