当前位置:编程学习 > C#/ASP.NET >>

关于c#两窗体间文本框的传值

建两个窗体,每个窗体各有一个textbox和butten ,当点form1 中的butten时,弹出form2,在form2中的textbox输入文字,点butten ,此时form1中的textbox内容和form2中的textbox一样。我做了如下代码,但是实现不了,form1中的textbox内容没有任何反映,请高手指点:
form1中代码:
  private void button1_Click(object sender, EventArgs e)
        {
            Form2 fm = new Form2();
            fm.Show();
            
        }
        public string t 
        {
            get { return this.textBox1.Text; }
            set { this.textBox1.Text = value; }
        }
form2中代码:
            private void button1_Click(object sender, EventArgs e)
        {  
            Form1 f = new Form1();
            f.t  = this.textBox1 .Text ;
        }
请高手指点,就用属性的方法传值,请不要用别的方法 
--------------------编程问答-------------------- 你在form2中是重新定义一个form1,当然不行了,你把新定义的form1显示出来就会看到已经赋值成功了!

  private void button1_Click(object sender, EventArgs e)
  {  
  Form1 f = new Form1();
  f.t = this.textBox1 .Text ;
  f.show();
  }
--------------------编程问答--------------------

  private void button1_Click(object sender, EventArgs e)
  {
  Form2 fm = new Form2(this);//定义fm的时候,要把form1对象传递过去
  fm.Show();
   
  }
  public string t 
  {
  get { return this.textBox1.Text; }
  set { this.textBox1.Text = value; }
  }
form2中代码:
  //增加定义一个变量保存form1
  private Form1 frm1;
  //修改构造函数
  public Form2(Form1 frm)
  {
    this.frm1=frm; 
  }
  private void button1_Click(object sender, EventArgs e)
  {  
    //对传递过来的from1对象进行修改
    this.frm1.t=this.textBox1 .Text ;
  }
--------------------编程问答-------------------- public partial class Form1 : Form  
  {  
  private void button1_Click(object sender, EventArgs e)  
  {  
  Form2 frm2 = new Form2();  
  frm2.Show(this);  
  }  
  }  

  public partial class Form2 : Form  
  {  
  private void button1_Click(object sender, EventArgs e)  
  {  
  Form1 frm1 = (Form1)this.Owner;  
  ((TextBox)frm1.Controls["textBox1"]).Text = this.textBox2.Text;  
  this.Close();  
  }  
  }  

public string t 
  {
  get { return this.textBox1.Text; }
  set { this.textBox1.Text = value; }
  }
frm1.t=this.textBox2.Text;  
--------------------编程问答-------------------- 学习一下, 哈哈, 谢谢 --------------------编程问答-------------------- 学习了
--------------------编程问答-------------------- 也可以使用委托于事件
 Form2 f = new Form2();
  private void button1_Click(object sender, EventArgs e)
  {
  f.getvalue += new Form2.getvalueHandler(f_getvalue);
  f.Show();
  }
  string f_getvalue()
  {
  return textBox1.Text;
  }
-----------------form2 代码-----------------------------------------
  public delegate string getvalueHandler();
  public event getvalueHandler getvalue;

  private void Form2_Load(object sender, EventArgs e)
  {
  textBox1.Text = getvalue();
  } 
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,