2011-10-18 646 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(); 
} 
相关问题