2011-03-22 131 views
0

我有一個主表單,可以從中生成多個子表單。我將這些表單存儲在List<Subform^> ^變量中,以便我可以從主窗體向他們發送消息。我加載新的形式是這樣的(從內存中,可能無法進行編譯):表單之間的通信

Subform ^sf = gcnew Subform(some, variables, here); 
subforms->Add(sf); 
subforms[subforms.Count-1]->Show(); 

我的目標是,一旦它的關閉,從列表中刪除子窗體。我一直在考慮轉移到字典更簡單的形式識別,如下所示:

++i; // Some sort of a form counter. to access them when closing. 
Subform ^sf = gcnew Subform(some, variables, here); 
subforms->Add(i, sf); 
subforms[i]->Show(); 

如何關閉時刪除第i個窗體?也許像這樣(僞代碼)

sf->FormClosed = subforms->RemoveAt[i]; // Before I add it to the dictionary. 

+0

你仍然可以使用,而不是一本字典的列表。但是,您必須遍歷列表才能找到要刪除的表單。 – 2011-03-22 16:10:51

回答

1

試着這麼做:

sf->FormClosed += gcnew FormClosedEventHandler(this, &RemoveSubform); 

void RemoveSubform(System::Object^ sender, FormClosedEventArgs^ e) 
{ 
    subforms->Remove(sender); 
}