2014-09-04 93 views
0

在我的applcation中,我有4個窗體,form1是一個mdi容器,其餘窗體是childs, 在form1中,我打開所有子窗體的加載事件。在子窗口2,我有一個按鈕,就會切換到childform3.my問題是如何當你創建你的窗口2顯示childform2如何顯示(Bringtofront)已從另一個Mdi兒童窗體打開的Mdi兒童窗體?

   form1: 

       Form2 formchild1; 
       Form3 formchild2; 
       Form4 formchild3; 

     private void Form1_Load(object sender, EventArgs e) 
     { 


     if (formchild2 == null) 
     { 
      formchild2 = new Form3(); 
     } 
     formchild2.MdiParent = this; 
     formchild2.Dock = DockStyle.Fill; 
     formchild2.Show(); 
     //formchild2.BringToFront(); 


     if (formchild3 == null) 
     { 
      formchild3 = new Form4(); 
     } 
     formchild3.MdiParent = this; 
     formchild3.Dock = DockStyle.Fill; 
     formchild3.Show(); 


     if (formchild1 == null) 
     { 
      formchild1 = new Form2(); 
     } 
     formchild1.MdiParent = this; 
     formchild1.Show(); 
     formchild1.Dock = DockStyle.Fill; 
     formchild1.BringToFront(); 


     } 


     form2: 

     Form3 formchild2; 
    private void button1_Click(object sender, EventArgs e) 
    { 
     //what i have to write hare.. 
      //formchild2 = new Form3(); 
      //formchild2.MdiParent = this.ParentForm; 

      //formchild2.Dock = DockStyle.Fill; 
      //formchild2.Show(); 
      //formchild2.BringToFront(); 

    } 
+0

如果我在按鈕中初始化新實例,它將在每次點擊按鈕時創建新的窗體實例。 – user3413736 2014-09-04 07:48:16

+0

讓一個孩子知道另一個表單是一個非常容易的做法。將此代碼放入MDI父窗體中,它必須已經知道您的所有子窗體。只需從孩子調用它,就像((MyMDIParent)this.MdiParent).FooBar()。 – 2014-09-04 09:49:53

+0

我沒有得到你的解釋,請發送更多的代碼,謝謝你 – user3413736 2014-09-04 12:16:50

回答

0

從按鈕childform3(這是已經打開)(請改變你的變量名,它花了一段時間來弄清楚,formchild1竟是窗口2),你需要實例化窗口2實例

if (formchild1 == null) 
{ 
    formchild1 = new Form2(/*Either pass in a Form3 here*/); 
} 
formchild1.formChild2 = formchild2; //Or make formChild2 public member 
formchlid1.SetForm(formChild2); //Or make a method that sets it 
formchild1.MdiParent = this; 
formchild1.Show(); 
formchild1.Dock = DockStyle.Fill; 
formchild1.BringToFront(); 

然後再次顯示它你可以做

formchild2.BringToFront(); 
0
childform3 childform3=new childform3; 

private void button1_Click(object sender, EventArgs e) 
    { 
     if (!childform3.IsDisposed) 
      childform3.Select(); 
     else 
      childform3= new frmSearch(); 
     childform3.MdiParent = ParentForm; 
     childform3.Show(); 
    }