2011-11-01 118 views
9

我有一個MDI表格。這MDI表單中,我可以使用打開一些子窗體:從另一個子窗體打開子窗體並將MDI設置爲父窗體 - 如何操作?

這是內MainForm

Form1 f1 = new Form1; 
f1.MdiParent = this; //this refers to MainForm (parent) 
f1.Show(); 

可正常工作!

但現在,當我在子窗體(Form - > F1)我想打開另一種形式爲孩子MainForm但是當我使用this關鍵字將reffer到f1。如何在f1內打開新窗體並將其MdiParent設置爲MainForm

+1

你可能可以使用this.MdiParent。通常最好留下創建子窗口給父母。使用事件告訴它採取行動。 –

回答

30

嘗試分配你的第一個孩子的父窗體從:

Form2 f2 = new Form2; 
f2.MdiParent = this.ParentForm; //this refers to f1's parent, the MainForm 
f2.Show(); 

希望這會有所幫助。

11

讓我們假設第二種形式是f2.Then,在形式上F1的代碼來創建MDI父窗體的新形式F2將是:

Form2 f2 = new Form2; 
f2.MdiParent = this.MdiParent; 
f2.Show(); 
3

好,不與被列入了「解決方案」爭論......但如果我理解正確的請求,並試圖將上述溶液沒有工作,我會做以下....

Form2 f2 = new Form2(); 
     f2.MdiParent = MDIParent1.ActiveForm; 
     f2.Show(); 
相關問題