1,
form2 fm = new form2();
fm.show();form1.fm1 = new form1();
fm1.xx = "xxx";//xx必须是public类型变量

解决方案 »

  1.   

    不行阿,请看:
    Form1 主要代码:
    ------------------------------------------------------------------------
    public int a=0;
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    Form2 fm=new Form2 ();
    fm.Show ();
    MessageBox .Show (a.ToString ());
    } }
    }
    ----------------------------------------------------------------------Form2主要代码:
    --------------------------------------------------------
    private void Form2_Load(object sender, System.EventArgs e)
    {
    Form1 fm =new Form1 ();
    fm.a=10;
    }
    -------------------------------------------------------------
    结果MessageBox显示FORM1中的a还是0;
      

  2.   

    你再做一个按纽,在事件中写MessageBox.Show(a.ToString())啊!
      

  3.   

    试试这样form1
    form2 fm = new form2();
    fm.fm = this;
    fm.show();form2public form1 fm = new form1();
    fm.a = "sdfsfa";
      

  4.   

    fm.fm出错
    Form2 b并不包含对fm的定义!
      

  5.   

    public form1 fm = new form1();不是写在form_load里的,是写在class下的
      

  6.   

    你的form2定义为b??
    那么就
    b.fm = this;
      

  7.   

    我发现一个问题就是
    如果FORM1是启动窗口
    那么FORM1可以更改FORM2里面的变量;
    而FORM2就不可以更改FORM1里面的变量;
      

  8.   

    全部代码如下:
    FORM1.CS
    //-------------------
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace WindowsApplication8
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    { public int a=10;
    private System.Windows.Forms.Button button1;
    /// <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.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(64, 112);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(152, 56);
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.button1);
    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 fm=new Form2 ();
    fm.Show ();
    } private void button1_Click(object sender, System.EventArgs e)
    {
    MessageBox.Show (a.ToString ());
    }
    }
    }
    //--------------------
    FORM2.CS
    //--------------------using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;namespace WindowsApplication8
    {
    /// <summary>
    /// Form2 的摘要说明。
    /// </summary>
    public class Form2 : System.Windows.Forms.Form
    {
    Form1 fm=new Form1 ();
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form2()
    {
    //
    // 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()
    {
    // 
    // Form2
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Name = "Form2";
    this.Text = "Form2";
    this.Load += new System.EventHandler(this.Form2_Load); }
    #endregion private void Form2_Load(object sender, System.EventArgs e)
    {
    fm.a=10000;
    }
    }
    }
      

  9.   

    FORM1.CS
    //-------------------
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace WindowsApplication8
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    { public int a=10;
    private System.Windows.Forms.Button button1;
    /// <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.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(64, 112);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(152, 56);
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.button1);
    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 fm=new Form2 ();
                               fm.fm1 = this;
    fm.Show ();
    }FORM2.CS
    //--------------------using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;namespace WindowsApplication8
    {
    /// <summary>
    /// Form2 的摘要说明。
    /// </summary>
    public class Form2 : System.Windows.Forms.Form
    {
    Form1 fm=new Form1 ();
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
      
    public Form1 fm1 = new Form1(); public Form2()
    {
    //
    // 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()
    {
    // 
    // Form2
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Name = "Form2";
    this.Text = "Form2";
    this.Load += new System.EventHandler(this.Form2_Load); }
    #endregion private void Form2_Load(object sender, System.EventArgs e)
    {
    fm1.a=10000;
    }
    }
    }
    private void button1_Click(object sender, System.EventArgs e)
    {
    MessageBox.Show (fm1.a.ToString ());
    }
    }
    }
      

  10.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    MessageBox.Show (a.ToString ());
    }
    }
    }
    这段是form1.cs的
      

  11.   

    public class Form2 : System.Windows.Forms.Form
    {
    Form1 fm=new Form1 ();
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
      
    public Form1 fm1 = new Form1();  //form2.cs在这个地方定义公共变量
      

  12.   

    为什么加上fm.fm1=this就可以了呢?
      

  13.   

    把自已赋予form2的fm变量,好了,我去睡了,明天还得上班呢:-)
      

  14.   

    1.就在MainForm的Main()里面加上
    BeginForm b = new BeginForm();
    Application.Run(b);
    加在Application.Run(new MainForm())的前面
    你也可以加上一个if语句,可以判断是不是要启动主窗体
    建议你用这样的顺序,不然会比较麻烦的2.要想更改一个窗口的变量,可以在这个窗口里面加一个自定义属性(把这个变量当一个属性),然后在另一个窗口里面set就可以了3.现在N多断口扫描器
      

  15.   

    to Reeezak:
    3.我上面没有说清楚,我说的测试端口是否打开是指如何用C#实现,而不是借助其他软件
    请问有什么办法吗?
    就是我要做一个程序(如APP1)
    程序(APP1)运行的时候就打开一个特定的端口监听
    而另外一个程序(APP2)运行的时候,就要扫描一次局域网里所有的计算机
    已确定有那些计算机在运行APP1
    谢谢!