小小菜鸟小小回复一下在form1添加一个lable 其text值等于form2中的textbox值form1的button中写show(form2)form2的button中写this.close()应该是没有问题的吧...

解决方案 »

  1.   

    form2的button中写this.close()
    我就是这么写的呀,然后就出现上面的问题了
      

  2.   

    把button方法重写了
    因为Form1中的button的方法new 一个form,同样你点继承他的窗口点button也会new一个窗口出来
      

  3.   

    Form1中的代码
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            test t=new test();
            private void button1_Click(object sender, EventArgs e)
            {            string a = textBox1.Text;
                Form2 f2 = new Form2(a,t);
                f2.ShowDialog();
                textBox1.Text = t.A;
                
                
            }
        }
    }
    Form2的代码
    namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
               
            }
            private string a;
            test t;
            public Form2(string a,test t)
            {            this.a = a;
                this.t = t;
                InitializeComponent();
            }        private void Form2_Load(object sender, EventArgs e)
            {
                textBox1.Text = a;
            }        private void button1_Click(object sender, EventArgs e)
            {
                t.A = textBox1.Text;
                this.Close();
            }
        }
    }
    另外要添加一个辅助类  class  test
    namespace WindowsFormsApplication1
    {
        public class test
        {
            private string a;        public string A
            {
                get { return a; }
                set { a = value; }
            }
        
        }
    }自己研究下吧  写的太清楚了你看过以后又会忘记了
      

  4.   

    Form1中的代码
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            test t=new test();
            private void button1_Click(object sender, EventArgs e)
            {            string a = textBox1.Text;
                Form2 f2 = new Form2(a,t);
                f2.ShowDialog();
                textBox1.Text = t.A;
                
                
            }
        }
    }
    Form2的代码
    namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
               
            }
            private string a;
            test t;
            public Form2(string a,test t)
            {            this.a = a;
                this.t = t;
                InitializeComponent();
            }        private void Form2_Load(object sender, EventArgs e)
            {
                textBox1.Text = a;
            }        private void button1_Click(object sender, EventArgs e)
            {
                t.A = textBox1.Text;
                this.Close();
            }
        }
    }
    另外要添加一个辅助类  class  test
    namespace WindowsFormsApplication1
    {
        public class test
        {
            private string a;        public string A
            {
                get { return a; }
                set { a = value; }
            }
        
        }
    }自己研究下吧  写的太清楚了你看过以后又会忘记了
      

  5.   

    前两天做毕设的时候碰到过类似的问题   
    你可以在Form1中声明一个变量用来存储Form2中输入的文本 例如:String myInput = "";
    在Form2构造函数中添加一个参数   private Form1 f1;          public Form2(Form1 f){this.f1= f;}
    在调用的时候创建对象 Form2 f2 = new Form2 (this);   f2.ShowDialog();  
    点击关闭按钮的事件
    private void closeButton_click (Object sender,EventArgs e)
    {
       f1.myInput = textbox1.text;
       this.Dispose();}
    回到Form1就可以使用myInput了   说白了就是把Form1的当前对象当做参数传给Form2  这样两个窗体就可以交互数据了
      

  6.   

    不用写的太复杂了,看我下面的例子:Form1中的代码:using 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 test01
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent(); 
            }
            
            public static string Text1;       //定义字符串,接收form2中textbox的值        private void button1_Click(object sender, EventArgs e)
            {
                Form2 sh = new Form2();      //点击按钮,显示form2
                sh.ShowDialog();
                this.label1.Text = Text1;    //把从form2中接收过来的值显示在label上
            }                                                                      
        }
    }
    再看form2的代码:using 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 test01
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Form1.Text1 = this.textBox1.Text;     //把输入到textbox的值传给form1中定义的Text1字符串
                this.Close();                         //退出
            }                                               
            }
    }