新建一个项目
在Program.cs文件中一般都是
Application.Run(new frm1());这是我在frm1上加一个按钮并写一个时间让new 一个frm2出来并同时让frm1自动关闭问题出现在实例话frm2并show以后的代码:
this.close();你可以试试,程序fail;我猜想是不是因为frm1本身并没有实例化造成的?因为我把断点设在this.close();上会调试到frm1.designer.cs中的这个函数上
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

解决方案 »

  1.   

    另外我还想知道每个frm后面的designer文件中的
    private System.ComponentModel.Container components = null;
    和函数
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    的作用...
    虽然朦朦胧胧的知道是干什么的,但是不明确
      

  2.   

    From MSDN: When a form is closed, all resources created within the object are closed and the form is disposed. 所以在单线程的程序里关闭主Form的时候,程序就结束了。
      

  3.   

    自我纠正一下
                Application.Run(new frm1());
    就已经实例化了一个frm1.......
      

  4.   

    但是他同时生成了frm2的一个对象啊那如果我想达到说"frm2.show的同时frm1自动关闭"应该怎么做?不dispose?
      

  5.   

    突然想到可以this.Visible = false;这样就可以在不释放的情况下达到看不到frm1的效果了....
    不过我们老大想了一个更好的方法,
    就是把主frm设置成frm2,而只是通过new frm1...
      

  6.   

    Application.Run(new ApplicationContext());
    form1做启动窗口,不设主窗口
      

  7.   

    private void button4_Click(object sender, System.EventArgs e)
    {
    Application.Run(new Form());
    this.Close();
    }
    楼主是不是,向上面这么写的,其实错不再this.Close(),而在Application.Run可以改成
    [STAThread]
    public static void Main()
    {
    new Form1().Show();
    System.Windows.Forms.Application.Run();
    }private void button4_Click(object sender, System.EventArgs e)
    {
    new Form2().Show();
    this.Close();
    }
    最后退出的时候,别忘了Application.Exit();
      

  8.   

    不是楼上那么写的,呵呵,那样的话真有点傻了不过下面给的soluation不错,谢谢
      

  9.   

    另外我还想知道每个frm后面的designer文件中的
    private System.ComponentModel.Container components = null;
    和函数
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    的作用...
      

  10.   

    System.ComponentModel.Container是組件的一個接口,就是說,如果form關閉後,它的所有組件也要被釋放。這樣說你就明白了吧。
      

  11.   

    form的关闭会引起什么操作导致这个组建被释放?
      

  12.   

    在什么时候用Application.Exit();???
      

  13.   

    希望在关闭应用程序的时候啊,呵呵,主窗口关闭则所有对象都被释放,如果不是主窗口在某些时候被关闭的时候希望退出应用程序就可以用exit(),例如你在登陆窗口中(非主窗口,可以用showdialog()的返回值确定是否打开主窗口),如果在登陆窗口中输入了3次,但都不对,就可以exit()了,或者在登陆口中点了"取消"按钮,也可以来退出程序
      

  14.   

    可以这么写,根据第一个显示窗口的dialogresult返回值,来确定是否显示接下来的窗口
    [STAThread]
    static void   Main() 
    {
    FormLogin fl=new FormLogin();
    fl.ShowDialog();
    if(fl.DialogResult==DialogResult.OK)
    {
    FormBookSel fBkSel=new FormBookSel();
    fBkSel.ShowDialog();
    if(fBkSel.DialogResult==DialogResult.OK)
    {
    FormRecite fRct=new FormRecite();
    Application.Run(fRct);
    }
    }

    }