Form1.cs
{
Form2 f2 = new Form2();
}
Form2.cs
{
Form1 f1 = new Form1();
}
这样的陷阱问题怎么解决???

解决方案 »

  1.   

    Form1.cs
    {
    Form2 f2 = new Form2(this);
    }Form2.cs
    构造函数里:Form2(Form1 obj)
    {
    this.Form=obj;
    obj.Form=this;
    }
      

  2.   

    这个方法好象不行,this.Form是什么意思?
      

  3.   

    你在form1和form2里便定义一个变量form通过form这样子引用对方
      

  4.   

    Form1.csusing System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace CTest
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
            private System.Windows.Forms.TextBox textBox1;
            private Form2 Form;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <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.SuspendLayout();
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(82, 123);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(128, 21);
                this.textBox1.TabIndex = 0;
                this.textBox1.Text = "textBox1";
                this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
                // 
                // Form1
                // 
                this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
                this.ClientSize = new System.Drawing.Size(292, 266);
                this.Controls.Add(this.textBox1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                this.ResumeLayout(false);        }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    }        private void Form1_Load(object sender, System.EventArgs e)
            {
                Form2 f = new Form2(this);
                this.Form =f;
                f.Show();
                this.textBox1.Text="";
            }        private void textBox1_TextChanged(object sender, System.EventArgs e)
            {
                this.Form.Text=this.textBox1.Text;
            }
    }
    }
      

  5.   

    Form2.cs:using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;namespace CTest
    {
    /// <summary>
    /// Form2 的摘要说明。
    /// </summary>
    public class Form2 : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
            private System.Windows.Forms.TextBox textBox1;
            private Form1 Form; public Form2()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    }
            public Form2(Form1 obj)
            {
                this.Form =obj;
                this.InitializeComponent();
            } /// <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.SuspendLayout();
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(86, 123);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(120, 21);
                this.textBox1.TabIndex = 0;
                this.textBox1.Text = "textBox1";
                this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
                // 
                // Form2
                // 
                this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
                this.ClientSize = new System.Drawing.Size(292, 266);
                this.Controls.Add(this.textBox1);
                this.Name = "Form2";
                this.Text = "Form2";
                this.Load += new System.EventHandler(this.Form2_Load);
                this.ResumeLayout(false);        }
    #endregion        private void textBox1_TextChanged(object sender, System.EventArgs e)
            {
                this.Form.Text=this.textBox1.Text;
            }        private void Form2_Load(object sender, System.EventArgs e)
            {
                this.textBox1.Text="";
            }
    }
    }