我用C#做了2个Form1(父),Form2。现在我要把Form1里面的textbox内容传递到Form2的一个textbox里面。
  请问:怎样传递过去?要写一个方法吗?
请给你的思想和代码?谢谢!!

解决方案 »

  1.   

    建一个类
    类中用一个静态字段存储把Form1里面的textbox的值
      

  2.   

    Form1 frn = new Form1 (你要的传的值)然后在Form2的窗体构造函数中
              值的类型 strs;
            public Form1(值的类型 str)
            {
                InitializeComponent();
                this.strs = str;
            }
      

  3.   

    Form2 frn = new Form2 (你要的传的值)
    frn.show();然后在Form2的窗体构造函数中
      值的类型 strs;
      public Form1(值的类型 str)
      {
      InitializeComponent();
      this.strs = str;
      }
    上面的错了!
      

  4.   

    窗体间传值的问题
    方法很多,如你在Form1 中设置一个全局的属性,再From2中访问。
    或者你直接封装From1 的那个TextBox,在From2中用From1 的对象访问它的Text值
    如:
    Public MyTextBox
    {
       get {retrun textBox1;}
    }
    在from2中
    用From1的对象(可以通过构造函数传)frm.MyTextBox.Text赋值给From2的文本框
      

  5.   

    form间传值
    通过公共静态类进行传值;  
    通过绑定事件进行传值;  
    使用Attribute  
    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();  
      }  
      }  
      

  6.   

    定义一个public变量,存放 Form1里面的textbox(public string  str)在form2中  声明form1的对象,调用刚才的变量,赋值给form2中的textboxfrom1 obj=new from1();
    textbox.Text=obj.str;
      

  7.   

    方法有很多!
    一:把form1和form2中的textbox访问级别都由private 该为publict直接用控件传值
    textbox.modifiers=public;
    //Form1中:
    Form f2=new Form2();
    f2.owner=this;//Form2中
    Form1 f1=(Form1)this.owner;
    this.textbox.Text=f1.textbox.Text;
    二:利用属性传值
    //在Form1中设置一个全局静态的字段
    例如:
    public static string content;
    content=textbox.Text;
    //直接在Form2中写
    this.textbox.Text=Form1.Content;
    三:利用事件
    该方法比较麻烦,一般情况下建议不要使用
    //声明事件所用到的类 声明在Form1中
    Class EventArgesGet:EventArges
    {
       private  string content;
       public EventArgesGet(string s)
       {
           content=s;
       }
       public string Contnt
       {
         get{return content;}
       } set{content=value; };
    }
    //声明事件所需的委托 声明在Form1中
    public void delete myGetEventHandler(object sender,EventArgesGet e);
    //事件声明 声明在Form1中
    public event mydelete myGet;
    //可在在Form1中的某个方法或事件中写上事件的激发:myget(this,new EventArgesGet(this,textbox.Text));
    //在Form2中声明事件所代理的方法
    pub void getValue(object sender,EventArgesGet e)
    {
     this.text.Text=e.Content;
    }
    //然后追加事件
    Form1 f1=new Form1();
    f1.myGet+=new myGetEventHandler(getValue);public voi set(object sender,EventArgesGet e)
    {
       this.textbox.Text=e.Content;
    }
      

  8.   

    不好意思上述事件声明关键字有问题:
    应为:
    //声明事件所需的委托 声明在Form1中
    public  delegate myGetEventHandler(object sender,EventArgesGet e);
      

  9.   

    不好意思上述事件声明关键字有问题:
    应为:
    //声明事件所需的委托 声明在Form1中
    public delegate void myGetEventHandler(object sender,EventArgesGet e);