两个窗体form1和form2
form1 里面textbox1
form2  里面textbox2  button1
按 button1 form2  里面textbox2 的值 传给form1 里面textbox1
最好针对这个例子给代码

解决方案 »

  1.   

    将form2窗体中的Text1的Modifiers属性设置为Publicusing System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            Form2 frm2;
            private void Form1_Load(object sender, EventArgs e)
            {
                frm2 = new Form2();
                frm2.Show();
            }        private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show(frm2.textBox1.Text);
            }
        }
    }
      

  2.   

    这样也可以://FORM1的代码
    Form2 frm2;
            private void Form1_Load(object sender, EventArgs e)
            {
                frm2 = new Form2();
                if (frm2.ShowDialog() == DialogResult.OK)
                {
                    MessageBox.Show(frm2.textBox1.Text);
                }
            }
    //FORM2的代码
     private void button1_Click(object sender, EventArgs e)
            {
                this.DialogResult = DialogResult.OK;
            }
      

  3.   

        public partial class Form1 : Form
        {
            public static TextBox tb1;
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                tb1 = textBox1;
            }
        }    public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Form1.tb1.Text = this.textBox2.Text;
            }
        }
      

  4.   

    因为Form1里面还有其他的数据,如果实例化FORM1,那么洽谈的数据就过不去。有没有其他的办法?
      

  5.   

    1、通过定义属性,应该可以做到吧;
    2、窗体应该也可以传值的吧。如:    frm2 = new frm2(int i);第一个窗体
            private void button1_Click(object sender, EventArgs e)
            {
                frm2 f = new frm2(int i);
                frm2.Show();
            }第二个窗体
            public frm2(int j)
            {
                InitializeComponent();
            }
      

  6.   

    1 全局变量(不会自动更新)
    2 用代理(最好的办法)
    3 通过数据库传值
    4 通过配置文件传值
    5 通过构造函数传值
    asp.net
    6 通过 session 传值
    7 通过 Url 传值
    8 通过 Response传值
    ........