Form1是主窗体,就是打开程序后显示后的第一个窗体。它关闭后,其它窗体都会关闭,而且整个程序就退出了。Form1里有一个TextBox1和一个按钮Button1。

private void Button1_Click(object sender,EventArgs e){Form2 f2=new Form2();f2.Show();}
打开这个Form2里有一个TextBox2和一个按钮Button2。那么,如何把这个TextBox2的内容,传递给主窗体Form1的TextBox1?private void Button2_Click(object sender,EventArgs e){把Form2的TextBox2的内容,传递回主窗体Form1的TextBox1里}请问,C#具体如何实现?

解决方案 »

  1.   

    給form2添加屬性,在form1創建form2的對象時,順便定閱form2的closing事件,然後在這個事件裏邊就可以拿到Form2中的屬性值form2 frm=new form2();
    frm.closing+=new eventhandle(方法名);
    frm.show();
    方法名(object sender,event e)
    {
     form2 frm=sender as form2;
     this.textBox1.text=frm.屬性;
    }
      

  2.   

    http://aierong.javaeye.com/blog/282892不好意思,忘记粘帖了,呵呵
      

  3.   


     public delegate void  FindResult(String text);        public event FindResult ResultFinded;        private void button1_Click(object sender, EventArgs e)
            {
                string qustring = this.textBox1.Text;
                SendFindInfo(qustring);        }
            private void SendFindInfo(string query,ref int pos)
            {
                if (ResultFinded != null)
                {
                    ResultFinded(query);
                }
            }
    主窗体    private void button1_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2();
                frm.ResultFinded += new Form2.FindResult(frm_ResultFinded);
                frm.Show();
                     }        void frm_ResultFinded(string text)
            {
                this.textBox1.text= text;
            }
      

  4.   

    如果要讓button2給form1賦值的話,你可以使用委拖傳參。
    public delete void SetMethod(string text);
    在Form2構造函數中寫個有參數的,接收委拖
    setMethod _d;
    public form2(SetMethod d)
    {
     _d=d;
    }Button2  click事件中
    {
      _d(textBox2.text);   這樣就行了
    }form1中  button事件
    {
     form2 frm=new form2(new setMethod(set));
     frm.show();
    }public void Set(string text)
    {
      textBox1.text=text;
    }
      

  5.   


    主窗口public string TB1
    {
          get { return textbox1.text; }
          set { textbox1.text = value; }
    }       Form2 nextForm1 = new Form2();
           nextForm1.Owner = this;
           nextForm1.ShowDialog();
    子窗口Form1 fatherForm = (Form1)this.Owner;
    Form1.TB1=this.textbox2.text;
      

  6.   

    你们这些都是转帖的吧?我早看过了。 其实我想实现Windows记事本一样的查找功能。就是要不停地点击Form2的Button2, 然后查找Form1的TextBox1的内容.什么关闭Form2后传递,肯定是不行。网上很多是错的,无用的。你试过就知道了。
      

  7.   

    主窗体:  private void button1_Click(object sender, EventArgs e)
      {
          Form2 frm2 = new Form2(this);
          frm2.Owner = this;
          frm2.Show();
       }第二个窗体:   public Form2(Form1 frmM)
       {
           InitializeComponent();
       }   private void button1_Click(object sender, EventArgs e)
       {
           Form1 frmM = (Form1)this.Owner;
           frmM.textBox1.Text = this.textBox2.Text;
       }把主窗体的textbox1的可见属性设为public就OK了