form1.cs代码: 
private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show();
        }
 
form2.cs代码: 怎么得到form1中textBox1的值?
private void button1_Click(object sender, EventArgs e)
        {
            Form1 frm1 = new Form1();            MessageBox.Show(frm1.textBox1.Text);    
    }
 

解决方案 »

  1.   

    你把FORM1的引用传到FORM2,然后TEXTBOX1权限是PUBLIC的
      

  2.   

    把form1中的textBox1的访问设置成public
      

  3.   


      public string test//
            {
                get { return this.textBox1.Text; }
            }
      

  4.   

    在Form1里面写入
    public string TextBoxValue
    {
        get
        {
              return textBox1.Text;
        }
    }在Form2里面去这么操作
    MessageBox.Show(Form1.TextBoxValue);
      

  5.   


    textBox1.modifiers =public 
    MessageBox.Show(frm1.textBox1.Text);    但是显示为空 为啥?
      

  6.   

    你FORM2里NEW的FORM1和产生FORM2的FORM1不是同一个
      

  7.   

    窗体传值可以分为两类。
    1、主窗体往子窗体传值
    有两种方法,一种是在子窗体提供重载构造函数,利用重载构造函数传递值,适用于传值数量比较少;第二种是,在子窗体中定义一个主窗体对象,然后就可以接收到主窗体的属性值了,适用于传值数量大。
    主窗体代码如下:
     public partial class frmParent : Form
        {
            private string strValueA = "";
            public string StrValueA
            {
                get
                {
                    return this.strValueA;
                }
                set { this.strValueA = value; }
            }
            public frmParent()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                this.strValueA = textBox1.Text;
                frmChild frmchild = new frmChild();
                frmchild.Owner = this;
                frmchild.ShowDialog();
                frmchild.Dispose();
            }        private void button2_Click(object sender, EventArgs e)
            {
                frmChild frmchild = new frmChild(this.textBox1.Text);
                string returnValue = "";
                if (frmchild.ShowDialog() == DialogResult.OK)
                {
                    returnValue = frmchild.Str;
                    this.textBox1.Text = returnValue;
                }
            }
        }
    子窗体代码如下:
    public partial class frmChild : Form
        {
            private string str;
            public string Str
            {
                get { return this.str; }
                set { this.str = value; }
            }
            private frmParent frmparent;        public frmChild()
            {
                InitializeComponent();
            }
            public frmChild(string str)
            {
                this.str = str;
                InitializeComponent();
                this.textBox1.Text = str;
            }
            private void frmChild_Load(object sender, EventArgs e)
            {
                frmparent = (frmParent)this.Owner;
                //this.textBox1.Text = frmparent.StrValueA;
            }        private void button1_Click(object sender, EventArgs e)
            {
                //frmparent = (frmParent)this.Owner;
                this.Str = this.textBox1.Text;
                this.DialogResult = DialogResult.OK;
                this.Close();
                
            }
        }
    2、从子窗体返回值到主窗体中
    利用了子窗体的属性保存子窗体的值,在主窗体中可以访问到子窗体的属性
    主窗体代码如下:
     public partial class frmParent : Form
        {
            private string strValueA = "";
            public string StrValueA
            {
                get
                {
                    return this.strValueA;
                }
                set { this.strValueA = value; }
            }
            public frmParent()
            {
                InitializeComponent();
            }
            private void button2_Click(object sender, EventArgs e)
            {
                frmChild frmchild = new frmChild(this.textBox1.Text);
                string returnValue = "";
                if (frmchild.ShowDialog() == DialogResult.OK)
                {
                    returnValue = frmchild.Str;
                    this.textBox1.Text = returnValue;
                }
            }
        }
    子窗体代码如下:
    public partial class frmChild : Form
        {
            private string str;
            public string Str
            {
                get { return this.str; }
                set { this.str = value; }
            }
            private frmParent frmparent;        public frmChild()
            {
                InitializeComponent();
            }
             private void frmChild_Load(object sender, EventArgs e)
            {
                frmparent = (frmParent)this.Owner;
                //this.textBox1.Text = frmparent.StrValueA;
            }        private void button1_Click(object sender, EventArgs e)
            {
                //frmparent = (frmParent)this.Owner;
                this.Str = this.textBox1.Text;
                this.DialogResult = DialogResult.OK;
                this.Close();
                
            }
        }
      

  8.   

    最好是一个方法,使用ref参数传递,接口显得紧凑(我向来封装为静态方法,连构造函数都不让外面看到)
    有时需要动态通知另一个窗体则发布事件,外面爱用就用,不用拉到~~~
      

  9.   

    我要在 form2 得到 frm1.textBox1 所有的属性值
    如 frm1.textBox1.Text  frm1.textBox1.name frm1.textBox1.width,,,等等  
      
      

  10.   

    1楼说的可以实现,不管你要什么值.你都可以自己取来相应的值来.public 
      

  11.   

    1: 所有权法
    //Form1:
    //需要有一个公共的刷新方法
    public void Refresh_Method()
    {
    //...
    }
    //在调用Form2时,要把Form2的所有者设为Form1
    Form2 f2 = new Form2() ;
    f2.Owner = this;
    f2.ShowDialog() ;
    //Form2:
    //在需要对其调用者(父)刷新时
    Form1 f1 ;
    f1 = (Form1)this.Owner;
    f1.Refresh_Method() ;eg:Form1中的函数: public void DiaoYong(string str)
            {
                this.textBox1.Text =str;
            }private void button2_Click(object sender, EventArgs e)
            {            string str = this.textBox1.Text;
                Form2 f2 = new Form2(str);//在构造函数中,向子窗体传值。
                f2.Owner = this;
                f2.ShowDialog();
            }Form2中的函数: public Form2(string ss)
            {
                InitializeComponent();
                this.textBox1.Text = ss;       } private void button1_Click(object sender, EventArgs e)
            {
                string st = textBox1.Text;            Form1 f1;
                f1 = (Form1)this.Owner;
                f1.DiaoYong(st);          this.Close();
            }
      

  12.   

    参考窗体的参数传递
    http://blog.csdn.net/zhzuo/archive/2006/05/05/708941.aspx#sec5
    http://blog.csdn.net/zhzuo/archive/2004/04/05/22027.aspx
      

  13.   

    在form1中的InitializeComponent()事件中,我们设置如下:
      public void InitializeComponent()
            {
                this.components = new System.ComponentModel.Container();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
                this.button1 = new System.Windows.Forms.Button();
                this.comboBox1 = new System.Windows.Forms.ComboBox();
                this.button2 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(2, 3);
                this.textBox1.Multiline = true;
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(183, 123);
                this.textBox1.TabIndex = 0;
                this.textBox1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseDoubleClick);
                // 
                // toolTip1
                // 
                this.toolTip1.ToolTipTitle = "我的tooltiptitle标题!";
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(110, 132);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 1;
                this.button1.Text = "获得驱动器";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // comboBox1
                // 
                this.comboBox1.FormattingEnabled = true;
                this.comboBox1.Location = new System.Drawing.Point(2, 132);
                this.comboBox1.Name = "comboBox1";
                this.comboBox1.Size = new System.Drawing.Size(102, 20);
                this.comboBox1.TabIndex = 2;
                // 
                // button2
                // 
                this.button2.Location = new System.Drawing.Point(110, 190);
                this.button2.Name = "button2";
                this.button2.Size = new System.Drawing.Size(75, 23);
                this.button2.TabIndex = 3;
                this.button2.Text = "打开Form2";
                this.button2.UseVisualStyleBackColor = true;
                this.button2.Click += new System.EventHandler(this.button2_Click);
                // 
                // main
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 270);
                this.Controls.Add(this.button2);
                this.Controls.Add(this.comboBox1);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.textBox1);
                this.Name = "main";
                this.Text = "main";
                this.Load += new System.EventHandler(this.main_Load);
                this.ResumeLayout(false);
                this.PerformLayout();        }        #endregion        public System.Windows.Forms.TextBox textBox1;
            public System.Windows.Forms.ToolTip toolTip1;
            public System.Windows.Forms.Button button1;
            public System.Windows.Forms.ComboBox comboBox1;
            public System.Windows.Forms.Button button2;
    注意其中的所有的public,这样的话,我们在form2界面中就可以这样使用了:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace test
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                main m = new main();
                this.label1.Text  = m.button2.Text ;
            }        private void Form2_Load(object sender, EventArgs e)
            {
                
                
            }
        }
    }
    注意看其中的button1_Click的代码.测试过了,完全可以的。
      

  14.   

    在上面的例子中,form1对应的是我的main
      

  15.   

    Windows窗体间的数据交互
    http://blog.csdn.net/zhzuo/archive/2004/04/05/22027.aspx
    窗体的参数传递
    http://blog.csdn.net/zhzuo/archive/2006/05/05/708941.aspx#sec5
      

  16.   

    最简单的方法:将TextBox的Modifier属性设置成public即可
      

  17.   

    使用ref参数传递,这是最简单,最好的办法.我以前用一个笨的办法就是在主窗体里打开子窗体时,将自己做为参数传递张子窗口,呵呵,有点像ref传递.
      

  18.   

    为什么不把form1的textBox1.text传给form2参数呢,大家为什么一定要搞些什么属性,那样Form2岂不是要创建Form1的一个对像,虽然传窗体可以,但是通常很少传窗体,除非用到Form1里的很多东西
    form1.cs代码:  
    private void button1_Click(object sender, EventArgs e) 
            { 
                Form2 frm2 = new Form2(textBox1.Text); 
                frm2.Show(); 
            } 
      
    form2.cs代码:
    添加一个变量string str;
    public Form2(string text)
    {
    str=text;
    }
    private void button1_Click(object sender, EventArgs e) 
            { 
                MessageBox.Show(str);     
        }