2017-11-18 211 views
0

我有一個問題,我不確定如何繼續並尋求一些方向。發送文本框文本到用戶控件

這裏是我的場景,我有一個帶有panel1的Form1,我可以在這些用戶控件的每一個裏面加載3個不同的用戶控件,它們位於panel1內部(UserControl1,UserControl2和UserControl3),我可以打開Form2,其中有幾個TextBoxes。

我需要的是每當我在Form2上按下按鈕時,所有TextBox文本都會發送到打開Form2的用戶控件。

我不確定這裏的問題是否清楚,如果有人能幫助我,我很感激,謝謝。

回答

0

這取決於您如何創建新表單。檢查this它包含兩種情況。

子窗體:

public partial class Child : Form 
    { 
     public event NotifyParentHandler NotifyParent; 
     public delegate void NotifyParentHandler(string textValue); 

     public Child() 
     { 
      InitializeComponent(); 
     } 

    private void btnNotify_Click(object sender, EventArgs e) 
    { 
     //assuming that you want to send the value when clicking a button 
     if (this.NotifyParent != null) 
     { 
      this.NotifyParent(textBox1.Text); 
     } 
    } 
} 

父窗體

public partial class Parent : Form 
    { 
     private Child childForm; 

     public Parent() 
     { 
      InitializeComponent(); 
     } 

    private void btnOpenChildForm_Click(object sender, EventArgs e) 
    { 
     // Open the child form 
     childForm = new Child(); 
     childForm.NotifyParent += childForm_NotificationTriggered; 
     childForm.ShowDialog(); 
    } 

    void childForm_NotificationTriggered(string textValuePassed) 
    { 
     //here you can do anything 
    } 
} 
+0

太感謝你了,它的工作完美! 你能告訴我你剛剛做了什麼,所以我可以詳細瞭解它嗎? 再次感謝。 – gpenner