2013-02-24 57 views
0

訪問MDI子控件我有一個MDIParent形式,MDIChild形式和所謂form1正常形態,form1從MDIChild繼承和,形成一個具有文本框名爲textBox1的,在父窗體我有兩個按鈕,新建和保存,當我點擊新建子窗體應加載,當我點擊保存一個消息框應彈出與textbox1.text價值,問題是消息框彈出與出使用波紋管代碼textbox1文本值從MDI父控件

IM在父窗體中加載子窗體。

public partial class MDIParent1 : Form 
{ 
    MdiClient mdi = null; 
    string fname; 

    public MDIParent1() 
    { 
     InitializeComponent(); 
     foreach (Control c in this.Controls) 
     { 
      if (c is MdiClient) 
      { 
       mdi = (MdiClient)c; 
       break; 
      } 
     } 
    } 
} 

和我使用來電使用波紋管代碼[點擊新按鈕]

private void ShowNewForm(object sender, EventArgs e) 
{ 
    load_form(new Form1()); 
} 

負載形式功能的負載形式的功能是

private void load_form(object form) 
{ 
    foreach (Form f in mdi.MdiChildren) 
    { 
     f.Close(); 

    } 
    if (form == null) 
     return; 
    ((Form)form).MdiParent = this; 
    ((Form)form).Show(); 
    ((Form)form).AutoScroll = true; 
    fname = ((Form)form).Name; 
} 

和我的形式被加載。 .in保存按鈕onClick功能,我打電話給form1功能叫做getdata()

public void getdata() 
{ 
    messageBox.show(textBox1.text); 
} 
+0

我真的必須指出,你的鑄造是完全沒有必要的。 「對象形式」應該是「表格形式」。 – 2013-02-24 07:05:10

+0

謝謝西蒙懷特黑德,但我的問題仍然沒有解決,PLZ可以幫助我的任何人 – Roshan 2013-02-24 07:21:53

回答

2
public partial class MDIChild : Form 
    { 
     public virtual string GetMessage() 
     { 
      return this.Name; 
     }  
    } 

    public class Form2 : MDIChild 
    { 
     TextBox textBox1 = new TextBox(); 

     public override string GetMessage() 
     { 
      return textBox1.Text; 
     } 
    } 


    public partial class MDIParent1 : Form 
    { 
     private MdiClient mdi = null; 
     private string fname; 
     private MDIChild currentActiveChild; 

     public MDIParent1() 
     { 
      base.InitializeComponent(); 
      foreach (Control c in this.Controls) 
      { 
       if (c is MdiClient) 
       { 
        mdi = (MdiClient) c; 
        break; 
       } 
      } 
     } 

     private void ShowNewForm(object sender, EventArgs e) 
     { 
      currentActiveChild = new Form2(); 
      load_form(currentActiveChild); 
     } 

     public void getdata() 
     { 
      if (currentActiveChild != null) 
      { 
       MessageBox.Show(currentActiveChild.GetMessage()); 
      } 
     } 
    } 
+1

我編輯答案檢查,如果這將是有用的朋友。 – 2013-02-24 10:55:45

+0

兄弟,我在我的form1中有一個文本框,我在設計時添加它,現在這段代碼在運行時創建新的文本框有一些使用這些文本框的混淆,我想讓我的設計時間文本框的值 – Roshan 2013-02-24 12:33:55

+1

實現GetMessage ()在你的手中,如果你有一個設計時間文本框的引用,那麼你可以基於使用該引用來改變GetMessage()的實現。如果遇到困難發送完整的代碼,我會盡力幫助你 – 2013-02-24 12:58:43