2016-03-05 142 views
-2

我想從子窗體發送到父窗體的類對象。請用example來幫助我。謝謝!如何將類對象子窗體發送到父窗體c#

ChildForm:

List<Person> person = new List<Person>(); 
     private void btnGonder_Click(object sender, EventArgs e) 
     { 
      SendItems(); 
      this.Close(); 

     } 
     public int SendItems() 
     { 
      Person pr = new Person(); 
      pr.telebe = tbTelebe.ToString(); 
      string telebe = tbTelebe.ToString(); 
      person.Add(pr); 
      Form1 parent = new Form1(); 
      parent.SendToParent(telebe); 
      return 0; 
     } 
+0

任何代碼........ ? – Ian

+1

SendItems();沒有父母形式的人類 –

+0

不,不......不過,您的變量名稱是「父母」,它不是「子形式」的「父母」。你在哪裏實例化父表單? – Ian

回答

0

試試這個....

ChildForm:

public partial class Form2 : Form 
{ 
    // private List of Person(s) 
    private List<Person> PersonsList = new List<Person>(); 

    // Public Read-only property that you can call from you Parent Form 
    public List<Person> Frm2PersonsList 
    { 
     get 
     { 
      return this.PersonsList; 
     } 
    } 

    public Form2() 
    { 
     InitializeComponent(); 
    } 

    private void btnGonder_Click(object sender, EventArgs e) 
    { 
     SendItems(); 
     this.Close(); 
    } 

    public void SendItems() 
    { 
     Person pr = new Person(); 
     pr.telebe = "Set in form 2"; // set you Person properties here 
     PersonsList.Add(pr); 
    } 
} 

ParentForm:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     Form2 frm2 = new Form2(); 
     frm2.ShowDialog(); 
     List<Person> Frm1PersonsList = frm2.Frm2PersonsList; // set a breakpoint here and step though the code to see the result from the Child Form 
    } 
} 
相關問題