MDI的子窗体如何得到父窗体的值
在父窗体实例化子窗体,然后将值传递过去,可是打开子窗体后变量还是NULL然后我就在子窗体中实例化父窗体,将值传递过来也不行
这是什么原因呢?

解决方案 »

  1.   

    newfrm frm = newfrm();
    frm.num = 你要传的值;
    frm.show();这样不就可以传过去了么,num在newfrm里面是public的
      

  2.   

    from1 thisowmer = (form2)this.Owner;
    this.textbox1.text = thisowner.aa; 
      

  3.   

    不应该这样,楼主贴点代码上来看看
    是不是传过值以后,子窗体又执行了什么初始化代码,将值还原成null 了
      

  4.   

    用委托在两窗体间传值的例子Using a delegate to pass data between two forms
      

  5.   

    1.子窗体读父窗体
    public string strChild;
    ...load(...)
    {
        FormParent formParent = new FormParent();
        strChild = formParent.strParent;
    }2.父窗体赋值给子窗体
    public string strParent;
    ...click(...)
    {
        FormChild formChild = new FormChild();
        formChild.strChild = strParent;
    }两种方法都试过了,单步跟踪后到子窗体的值都是NULL
      

  6.   

    实例化了就等于是个新的,肯定是空了,,
    要不用委托在两窗体间传值。
    要不在new form2的时候把form1直接传过去,然后在form2的构造函数里拿到form1,在拿form1里的值
      

  7.   

    父窗口调用子窗口,关闭子窗口将内容返回给父窗口
     //父窗口调子窗口函数       
    private void ShowLinkDBDialog(object sender, EventArgs e)
            {         
                //连接子对话框。
                fchild obj = new fchild(this);//this父窗口
                obj.WindowState = FormWindowState.Normal;
                obj.ShowDialog();
                this.TextBox1.Text = obj.RetValue;
            }
    子窗口相关内容
    private Form _parentForm=null;
            private static string _RetValue = null;
            public string RetValue
            {
                get { return _RetValue; }
            }
            //        
            public fVistSQLServer(Form parentForm)//带参数的构造函数
            {
                InitializeComponent();
                this._parentForm = parentForm;
            }
    //点确定按钮,返回信息到父窗口,并关闭子窗口.
    private void confirm_Click(object sender, EventArgs e)
            {
                _RetValue = this.TextBox1.Text.Trim();
    ((fparent)_parentForm).TextBox2.Text="123";            //关闭窗口
                    this.Close();                             
          }     
    注意:以上代码包含了两种父窗口传值给子窗口的过程。
      

  8.   

    2.父窗体赋值给子窗体 
    C# codepublic string strParent;
    ...click(...)
    {
        FormChild formChild = new FormChild();
        formChild.strChild = strParent;
    }
    修改一下你上面的代码
    //--------------------
    在子窗体中这样写:
    private string _strChild;
    public string strChild
    {
         set{_strChild=value;}
    }父窗口传值给子窗口...click(...)
    {
        FormChild formChild = new FormChild();
        formChild._strChild = strParent;//不知道你这个strParent的值是否存在,还是为NULL.
        formChild .MdiParent = FormParent.ActiveForm;//这句可有可无,根据情况而定
        formChild .WindowState = FormWindowState.Maximized;
        formChild .Show();
    }
      

  9.   

    父窗口调用子窗口,关闭子窗口将内容返回给父窗口 
    //父窗口调子窗口函数     private void ShowLinkDBDialog(object sender, EventArgs e) 
            {        
                //连接子对话框。 
                fchild obj = new fchild(this);//this父窗口 
                obj.WindowState = FormWindowState.Normal; 
                obj.ShowDialog(); 
                this.TextBox1.Text = obj.RetValue; 
            } 
     子窗口相关内容 private Form _parentForm=null; 
            private static string _RetValue = null; 
            public string RetValue 
            { 
                get { return _RetValue; } 
            } 
            //        
            public fVistSQLServer(Form parentForm)//带参数的构造函数 
            { 
                InitializeComponent(); 
                this._parentForm = parentForm; 
            } 
    //点确定按钮,返回信息到父窗口,并关闭子窗口. private void confirm_Click(object sender, EventArgs e) 
            { 
                _RetValue = this.TextBox1.Text.Trim(); 
    ((fparent)_parentForm).TextBox2.Text="123";             //关闭窗口 
                    this.Close();                            
          }  
      
    注意:以上代码包含了两种父窗口传值给子窗口的过程。
      

  10.   

    1.父窗口调用子窗口,关闭子窗口将内容返回给父窗口 
    //父窗口调子窗口函数    
    C# code
    private void ShowLinkDBDialog(object sender, EventArgs e) 
            {        
                //连接子对话框。 
                fchild obj = new fchild(this);//this父窗口 
                obj.WindowState = FormWindowState.Normal; 
                obj.ShowDialog(); 
                this.TextBox1.Text = obj.RetValue; 
            } 子窗口相关内容 
    C# code
    private Form _parentForm=null; 
            private static string _RetValue = null; 
            public string RetValue 
            { 
                get { return _RetValue; } 
            } 
            //        
            public fchild(Form parentForm)//带参数的构造函数 
            { 
                InitializeComponent(); 
                this._parentForm = parentForm; 
            }//点确定按钮,返回信息到父窗口,并关闭子窗口. 
    C# code
    private void confirm_Click(object sender, EventArgs e) 
            { 
                _RetValue = this.TextBox1.Text.Trim(); 
    ((fparent)_parentForm).TextBox2.Text="123";             //关闭窗口 
                    this.Close();                            
          }    
    注意:以上代码包含了两种父窗口传值给子窗口的过程。2.父窗体赋值给子窗体 在子窗体中这样写: C# code
    private string _strChild;
    public string strChild
    {
         set{_strChild=value;}
    }
    父窗口传值给子窗口 
    C# code
    ...click(...)
    {
        FormChild formChild = new FormChild();
        formChild._strChild = strParent;//不知道你这个strParent的值是否存在,还是为NULL.
        formChild .MdiParent = FormParent.ActiveForm;//这句可有可无,根据情况而定
        formChild .WindowState = FormWindowState.Maximized;
        formChild .Show();
    }
      

  11.   

    This code demonstrates how you can pass data from one form to another using a delegate. The advantage of using a delegate is that the form from which you want to send the data, doesn't need to know anything about the form that it's sending its data to. This way, you can reuse the same form in other forms or applications.
      

  12.   

    1 用委托
    2 在子窗口:
             public static Form1 fm1 = null;
            。
            。
            。
             public Form1()
            {
                InitializeComponent();
                fm1 = this;
            }
     父窗口就访问了
          Form1.fm1.*****
      

  13.   

    1.子窗体中定义一个公共变量curr_value
    代码如下:
    public string curr_value
    {
       get {return _curr_value;}
       set { value;}
    }2.父窗体代码如下:
       private FormChild frm = null;
       frm = new FromChild();
       frm.curr_value = "传递的值";