怎样在子窗体为父窗体的textbox赋值?父窗体public string strpath = ""; private void btnAddFtpFile_Click(object sender, EventArgs e)
        {
            string strip=txtFtpServer.Text.Trim().ToString();
            string strname=txtFtpUserName.Text.Trim().ToString();
            string strpwd=txtFtpPassword.Text.Trim().ToString();            frmFtpFileTree frm = new frmFtpFileTree(strip,strname,strpwd);
            frm.ShowDialog(); //打开子窗体            this.txtFtpPath.Text = strpath.ToString();
  //等子窗体关闭后,为textbox赋值。 但是子窗体关闭后还是赋值不了。        }
子窗体
  private void btnOK_Click(object sender, EventArgs e)
        {
            frmListFileAdd frm = new frmListFileAdd();            frm.strpath="waetrwe"; //为父窗体的公共变量赋值
          
            //this.Close();
        }
帮我看看

解决方案 »

  1.   

    我想要的就是在子窗体获取到值后,关闭子窗体,把值显示在父窗体的textbox
      

  2.   

    public string strpath = "";
    ------------------------------>public static string strpath = "";
    private void btnOK_Click(object sender, EventArgs e)
            {
                frmListFileAdd frm = new frmListFileAdd();            frm.strpath="waetrwe"; //为父窗体的公共变量赋值
              
                //this.Close();
            }
    ------------------------------>private void btnOK_Click(object sender, EventArgs e)
            {            frmListFileAdd .strpath="waetrwe"; 
     
            }
      

  3.   

    将父窗体SELF作为参数传入子窗体,然后用:
    window.document.getElementById("aaa");
    找到给赋值就OK了!
      

  4.   

    父窗体 
    private void button1_Click(object sender, EventArgs e)
            {
                Form2 f = new Form2(this);
                f.Show();
            }
    子窗体
    Form1 frm;
            public Form2(Form1 fr)
            {
                frm = fr;
                InitializeComponent();
            }        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
            {
                frm.textBox1.Text = "ddd";//textBox1是父窗体的一个控件, 要设为公有
            }
      

  5.   

     父窗体:
     private void btnAddFtpFile_Click(object sender, EventArgs e)
            {
                string strip=txtFtpServer.Text.Trim().ToString();
                string strname=txtFtpUserName.Text.Trim().ToString();
                string strpwd=txtFtpPassword.Text.Trim().ToString();            using(frmFtpFileTree frm = new frmFtpFileTree(strip,strname,strpwd))
                {
                   if(frm.ShowDialog()==DialogResult.OK)//打开子窗体
                   {
                       this.txtFtpPath.Text = frm.strpath;
                   } 
                }
            }
    子窗体:
      public string strpath = "";
      private void btnOK_Click(object sender, EventArgs e)
            {
                this.strpath="waetrwe"; //为父窗体的公共变量赋值
                  this.DialogResult = DialogResult.OK;
            }
      

  6.   

    frm.strpath="waetrwe"; //为父窗体的公共变量赋值

    你的值赋给了子窗体的变量。直接写strpath="waetrwe" 就赋上了
      

  7.   

    为什么public static string strpath = ""; 就可以 public string strpath = ""; 不行呢?
      

  8.   

    http://hi.baidu.com/libinguest/blog/item/0110fb1f077de96af624e4b0.html
    自己去看下,很详细的。
      

  9.   

    参考 http://blog.csdn.net/knight94/archive/2006/03/18/628285.aspx
      

  10.   

    我认为父窗体的控件或变量赋值操作放在子窗体里不利于维护,我一般是这样做的:
    在子窗体中加一个函数,形式是这样的:private string m_Result = "";  // 返回值
    public string ShowMe(这里可以加一些参数)
    {
        this.ShowDialog();
        return m_Result;
    }// 在父窗体中就这样调用就行了:
    txtControl.Text = frmChild.ShowMe(参数);
      

  11.   

    private void btnOK_Click(object sender, EventArgs e)
            {
                frmListFileAdd frm = new frmListFileAdd();            frm.txtFtpPath.Text="waetrwe"; //为父窗体的公共变量赋值
              
                  this.Close();
            }
      

  12.   

    以上改之前,需把父窗体的text属性改为public
      

  13.   

    form2中:C# codepublic string Form2Value 

        get 
        { 
            return this.textBox1.Text; 
        } 
        set 
        { 
            this.textBox1.Text = value; 
        } 

    public event EventHandler accept; 
    private void button1_Click ( object sender , EventArgs e ) 

        if ( accept != null ) 
        { 
            accept ( this , EventArgs.Empty );  
        } 

     form1:中
    Form2 f2 = new Form2 ( ); 
    f2.accept += new EventHandler ( f2_accept ); 
    f2.Show ( ); 
    void f2_accept ( object sender , EventArgs e ) 

        Form2 f2 = (Form2) sender; 
        this.textBox1.Text = f2.Form2Value; 
    }
    这样就可以了..
      

  14.   

    父窗体 Form1private void button1_Click(object sender, EventArgs e)
    {
                Form2 f2 = new Form2();
                f2.ShowDialog(this);
    }子窗体 Form2private void button2_Click(object sender, EventArgs e)
    {
                Form1 f1 = (Form1)this.Owner;
                f.textbox1.text = "aaaaaaaaa";
    }