你只要把FORM1中的BUTTON1的PRIVATE设为PUBLIC,同时把FORM1和FORM2放在同一个名字空间

解决方案 »

  1.   

    看   孟宪会  多个窗体之间如何互相调用在VB6中,多个窗体之间可以很方便地互相调用,如:在Form1中,只需要用一条“Form2.Show” 语句就能显示窗体Form2。然而在VB.NET中窗体处理机制发生了很大的变化:在访问窗体之前,你必须进行窗体实例化;如果在项目中有多处代码访问同一窗体,则你必须把它的同一实例指针传递给这些代码,否则新创建的窗体实例就不再是原先的窗体了。 下面的代码实现窗体Form1和Form2之间互相调用,Form1为主窗体。Form1上的按钮BtnShowFrm2的标题为“显示Form2”,Form2上的按钮BtnShowFrm1的标题为“显示Form1”。 1、Form1中的代码:Public Class Form1
    Inherits System.Windows.Forms.Form
    '创建Form2的一个新的实例
    Dim Frm2 As New Form2() Public Function Instance2(ByVal frm As Form2)
    Frm2 = frm
    End Function Private Sub BtnShowFrm2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles BtnShowFrm2.Click
    '以下语句保证在Form2以及其他窗体中访问Form1时,
    '都将得到Form1的同一个窗体实例。
    Frm2.Instance(Me)
    Frm2.Show()
    Me.Hide()
    End SubEnd Class2、Form2中的代码:Public Class Form2
     Inherits System.Windows.Forms.Form
     Dim frm1 As Form1
     '借助一个新增的Instance属性来生成窗体frm1的实例
     Public Function Instance(ByVal frm As Form1)
      frm1 = frm
     End Function Private Sub BtnShowFrm1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles BtnShowFrm1.Click
      Me.Hide()
      frm1.Show()
     End Sub Private Sub Form2_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
      '如果Form2被关闭,则设置Form1的按钮BtnShowFrm2不可用。
      frm1.BtnShowFrm2.Enabled = False
      frm1.Show()
     End Sub
    End Class
      

  2.   

    在Form1 BUTTON事件中加上
    Form2 Frm2=new Form2();
           Frm2.show();
           
    在Form2 BUTTON事件中加上Form1.show();this.Close();
      

  3.   

    晕,你新建两个窗口类,然后第一次启动的时候,把某个标识位置1,或者就show原来的窗口!  那个x按钮,你就重新写一下closing的消息处理函数! 不过好像关掉form2本来就不会结束application啊,因为form1的x按钮才会导致退出程序!
      

  4.   

    form1中的button1里面写
    private void button1_Click(object sender, System.EventArgs e)
    {
    FrmTwo Form2=new FrmTwo();
    Form2.Show();
    this.Hide();
    }
    FrmTwo(form2的类)中button1里面写
        private void button1_Click(object sender, System.EventArgs e)
        {
          this.Close();
        }
    FrmTwo(form2的类)中Closing时事件里面写
        private void FrmTwo_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
          Application.Exit();
        }
      

  5.   

    写完closing事件还需不需要在哪里引用?
      

  6.   

    http://dotnet.blogger.cn/iceshark/archive/2004/03/25/503.aspx
      

  7.   

    帮楼主写了一下
    form1.cs
    ==========================
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;namespace ZZ
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;

    private System.ComponentModel.Container components = null; public Form1()
    {
    InitializeComponent();
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(288, 72);
    this.button1.Name = "button1";
    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(396, 221);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion private void button1_Click(object sender, System.EventArgs e)
    {
    Form2 form2 = new Form2();
    this.Hide();
    form2.ShowDialog(this);
    form2.Dispose();
    this.Show();
    }


    }
    }
    ==================
    form2.csusing System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;namespace ZZ
    {
    /// <summary>
    /// Form2 的摘要说明。
    /// </summary>
    public class Form2 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.ComponentModel.Container components = null; public Form2()
    {
    InitializeComponent();
    } 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(204, 100);
    this.button1.Name = "button1";
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form2
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 153);
    this.Controls.Add(this.button1);
    this.Name = "Form2";
    this.Text = "Form2";
    this.ResumeLayout(false); }
    #endregion private void button1_Click(object sender, System.EventArgs e)
    {
    this.Close();
    }

    }
    }
      

  8.   

    这种问题在CSDN已经被问了N遍
      

  9.   

    谢谢大家,我已经搞定了!
    感谢楼上写了出来。form2还要写closing事件。我已经写好了!