2011-10-18 71 views
0

我有兩種形式,一種是按鈕'添加',它使用兩個文本框和一個提交按鈕加載第二種形式。從父窗體和子窗體傳遞參數值c#

首先我需要傳遞文本框的值,形成1(父母)對提交的方式和形式,使近2 ..

我怎樣才能做到這一點?到現在爲止我寫了這個代碼,但它不工作

private void button1_Click(object sender, EventArgs e) 
     { 

      emailForm EmailF = new emailForm(); 
      if ((EmailF.Username != null && EmailF.Password != null)) 
      { 
       string user = EmailF.Username; 
       string pass = EmailF.Password; 

      } 

並在你需要看看Form.ShowDialog()的emailForm.cs

private void button1_Click(object sender, EventArgs e) 
     { 
      username = username_textbox.Text; 
      password = username_textbox.Text; 
      Close(); 

     } 

     public string Username 
     { 
      get { return username; } 
      set { this.username = value; } 
     } 

     public string Password 
     { 
      get { return password;} 
      set { this.password = value; } 
     } 
+0

你是什麼意思,說「不工作」?您閱讀的屬性,例如EmailF.Username是否爲空? – Tigran

回答

2

。這將做你想做的事情,當用戶關閉對話窗口時,你可以確保他們點擊了「OK」(或其他),然後從表單中獲取值。

+0

但是當我點擊提交時,表單甚至沒有關閉。怎麼來的? – michelle

+0

確定它的工作,忘記讓它不可見。謝謝 – michelle

+1

呃...你應該使用'Close()',不要讓它看不見。 – qJake

0
public void ShowMyDialogBox() 
{ 
    Form2 testDialog = new Form2(); 

    // Show testDialog as a modal dialog and determine if DialogResult = OK. 
    if (testDialog.ShowDialog(this) == DialogResult.OK) 
    { 
     // Read the contents of testDialog's TextBox. 
     this.txtResult.Text = testDialog.TextBox1.Text; 
    } 
    else 
    { 
     this.txtResult.Text = "Cancelled"; 
    } 
    testDialog.Dispose(); 
}