在form2中将textBox3定义为public型
form1中Button1的function里开打form2前(form2.show());
form2.textBox3 = 你想传的内容!

解决方案 »

  1.   

    也可以是通过传递变量
    1、传数据集:在程序private .....dataset;
    public form2( ....dataset)
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();
    this.dataset=dataset;
                
    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    }
    2、传窗体过去,把dbgrid设置为public,在绑定过去
      

  2.   

    用属性吧
    Form1:
       Form2 f2=new Form2();
       f2.tb3=this.dataGrid1.....
       f2.Show()
       
    Form2:
       public string tb3
       {
           set
           {
              this.testBox3.Test=this.tb3;
           }
       }
      

  3.   

    用jackwin的方法有一个问题:我本来是form2.showdialog();是在所有窗口之上的,现在用form2.show;如果在其它窗口上一点,form2就不是在最上面了。有什么方法可以用showdialog又可以实现在窗体间传递变量的?
      

  4.   

    muForm1:
    private void button1_Click(object sender, System.EventArgs e)
    {
    Form2 myForm2= new Form2();
    myForm2.Show();
           myForm2.button1.Text="test from form";
    myForm2.button2Property="Assign using property";
             myForm2.myForm1=this;
             //myForm2.ShowDialog();

    }myForm2      public System.Windows.Forms.Button button1;  //注意public
          private System.Windows.Forms.Button button2; //注意private,通过属性来暴露
    public  Form1  myForm1;//应用myForm1
       public string button2Property
          {
             set
             {
                this.button2.Text =value;
             }
             get
             {
                return button2.Text;
             }
          }
    private void button1_Click(object sender, System.EventArgs e)
    {
    myForm1.textBox1.Text ="Assignment from myForm2";
                      //修改myForm1得值
    }
      

  5.   

    有什么方法可以用showdialog又可以实现在窗体间传递变量的?
     
    myForm2.Show();  //把这个注释调  
           myForm2.button1.Text="test from form";
    myForm2.button2Property="Assign using property";
             myForm2.myForm1=this;
             //myForm2.ShowDialog();  
               //把这个打开,注意,赋值在showmdialog以前进行
      

  6.   

    //在另一个窗口中修改当前选定的DataGrid的行
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace DataForms
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.DataGrid dataGrid1;
    private DataSet ds;
    private System.Windows.Forms.Button button1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    InitializeComponent();
    ds = new DataSet("MyDataSet");
    InitData(ds);
    this.dataGrid1.DataSource = this.ds;
    this.dataGrid1.DataMember = this.ds.Tables[0].TableName;
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    this.button1 = new System.Windows.Forms.Button();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    this.SuspendLayout();
    // 
    // dataGrid1
    // 
    this.dataGrid1.DataMember = "";
    this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid1.Location = new System.Drawing.Point(16, 20);
    this.dataGrid1.Name = "dataGrid1";
    this.dataGrid1.Size = new System.Drawing.Size(340, 160);
    this.dataGrid1.TabIndex = 0;
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(264, 196);
    this.button1.Name = "button1";
    this.button1.TabIndex = 1;
    this.button1.Text = "修改";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(372, 229);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.dataGrid1);
    this.Name = "Form1";
    this.Text = "Form1";
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.ResumeLayout(false); }
    #endregion private void InitData(DataSet ds)
    {
    DataTable dt = new DataTable("TabeHost");
    DataColumn dc = new DataColumn("IpAddress",typeof(string));
    dt.Columns.Add(dc);
    dc = new DataColumn("UserName",typeof(string));
    dt.Columns.Add(dc);
    dc = new DataColumn("Password",typeof(string));
    dt.Columns.Add(dc);
    ds.Tables.Add(dt);
        
    DataRow dr = dt.NewRow();
    dr["IpAddress"] = "192.192.132.229";
    dr["UserName"] = "zhzuo";
    dr["Password"] = "zhengzuo";
    dt.Rows.Add(dr); dr = dt.NewRow();
    dr["IpAddress"] = "192.192.132.230";
    dr["UserName"] = "11";
    dr["Password"] = "12";
    dt.Rows.Add(dr); dr = dt.NewRow();
    dr["IpAddress"] = "192.192.132.231";
    dr["UserName"] = "123";
    dr["Password"] = "12";
    dt.Rows.Add(dr); dr = dt.NewRow();
    dr["IpAddress"] = "192.192.132.232";
    dr["UserName"] = "22";
    dr["Password"] = "788";
    dt.Rows.Add(dr);
    } private void button1_Click(object sender, System.EventArgs e)
    {
    DataRowView drv = (DataRowView)this.BindingContext[this.ds,this.ds.Tables[0].TableName].Current;
    Form2 form2 = new Form2(drv);
    form2.ShowDialog(); }
    }
    }
    =======================
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace DataForms
    {
    /// <summary>
    /// Form2 的摘要说明。
    /// </summary>
    public class Form2 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private DataRowView drv;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form2(DataRowView dr)
    {
    InitializeComponent();
    this.drv = dr;
    this.textBox1.Text = (string)drv["IpAddress"];
    this.textBox2.Text = (string)drv["UserName"];
    this.textBox3.Text = (string)drv["Password"];
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.textBox3 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(28, 16);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 3;
    this.textBox1.Text = "textBox1";
    // 
    // textBox2
    // 
    this.textBox2.Location = new System.Drawing.Point(28, 56);
    this.textBox2.Name = "textBox2";
    this.textBox2.TabIndex = 4;
    this.textBox2.Text = "textBox2";
    // 
    // textBox3
    // 
    this.textBox3.Location = new System.Drawing.Point(28, 92);
    this.textBox3.Name = "textBox3";
    this.textBox3.TabIndex = 5;
    this.textBox3.Text = "textBox3";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(152, 152);
    this.button1.Name = "button1";
    this.button1.TabIndex = 6;
    this.button1.Text = "保存";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(236, 152);
    this.button2.Name = "button2";
    this.button2.TabIndex = 7;
    this.button2.Text = "退出";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // Form2
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(324, 201);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox3);
    this.Controls.Add(this.textBox2);
    this.Controls.Add(this.textBox1);
    this.Name = "Form2";
    this.Text = "Form2";
    this.ResumeLayout(false); }
    #endregion private void button1_Click(object sender, System.EventArgs e)
    {
    drv["IpAddress"] = this.textBox1.Text;
    drv["UserName"] = this.textBox2.Text;
    drv["Password"] = this.textBox3.Text;
    } private void button2_Click(object sender, System.EventArgs e)
    {
    this.Close();
    }
    }
    }
      

  7.   

    如果是传递数据
    http://blog.csdn.net/zhzuo/archive/2004/05/10/22032.aspx
    http://blog.csdn.net/zhzuo/archive/2004/05/10/22033.aspx
    http://blog.csdn.net/zhzuo/archive/2004/05/10/22034.aspx
      

  8.   

    用jackwin的方法有一个问题:我本来是form2.showdialog();是在所有窗口之上的,现在用form2.show;如果在其它窗口上一点,form2就不是在最上面了。有什么方法可以用showdialog又可以实现在窗体间传递变量的?
    那就直接用showdialog啦!晕