两个Form一个为LoginForm,一个为RegisterForm.
RegisterForm里面有一个btnCancel的按钮。
在btnCancel的Clink事件里写了Application.Exit();
为什么我在RegisterForm里面点击btnCancel,LoginFrom会跟着一起关闭

解决方案 »

  1.   

    Application.Exit -> this.Close
      

  2.   

    btnCancel的Clink事件里写了Application.Exit(); 
    换成this.Close();
      

  3.   

    Application.Exit(); 结束了当前进程,当然会把当前进程所有的窗口关闭了。
      

  4.   

    Application是多么牛B的东西,你退出了整个应用程序,谁还罩的住你的一个小Form
      

  5.   

    补充一下一点不好意思
    那LoginForm里面有一个lblRegister标签在他的Clink事件里面我写了
     RegisterForm rf = new RegisterForm();
                rf.Show();
                this.Close();
    为什么我点击lblRegister的时候两个窗体都关了。
    确认一点我要的是关闭LoginForm,打开RegisterForm
    而不是把LoginForm隐藏
      

  6.   

    你把启动的form改成RegisterForm不就得了,在RegisterForm 的LOAD事件里先让它隐藏一下,让
    LoginForm先出来,验证成功后显示不就好了。
      

  7.   

    你的Main函数写在LoginForm中了,释放LoginForm资源就会执行Application.Exit(); 
      

  8.   

    Application.Exit 是结束程序
    this.Close       是关闭当前窗体
      

  9.   

    application.Exit()方法是退出你写的应用程序应该用this.close()
      

  10.   

    为什么我点击lblRegister的时候两个窗体都关了。
    Application.Exit()// 结束了当前进程,
    我要的是关闭LoginForm,打开RegisterForm 
    RegisterForm rf = new RegisterForm();
                rf.Show();
                this.Close();你这就是这个功能啊!
    LoginForm隐藏//this.Hide();
      

  11.   

    因为你关闭的是主窗体。系统每次运行都会有一个默认的主窗体。也就是program.cs里run起来的那个窗体。hide一下吧,也不是什么丢人的事。
      

  12.   

    Application.Exit -> this.Close
      

  13.   


    因为主窗口的关闭会导致主线程的消息循环被中止,所以,你应该在LoginForm关闭之前将RegisterForm设置为主窗口。
      

  14.   

    Application.Exit();
    整个工程退出~this.close()本窗口退出
      

  15.   

    Application.Exit -> this.Close
      

  16.   

    因为主窗口的关闭会导致主线程的消息循环被中止,所以,你应该在LoginForm关闭之前将RegisterForm设置为主窗口。
      

  17.   

    我想到也是这点就是把Application.Run(new LoginForm());
    改成Application.Run(new RegisterForm());
    但运行起来还是不对。。希望大家帮我解释我错在哪里了,谢谢了
    Main()方法
              static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);            Application.Run(new LoginForm());
                LoginForm lf = new LoginForm();
                if (lf.DialogResult == DialogResult.OK) 
                {
                    Application.Run(new RegisterForm());
                    lf.Close();
                }
            }
    在LoginForm linkLabel的Click的事件里面
    RegisterForm rf = new RegisterForm()
    rf.Show();
    this.DialogResult = DialogResult.OK;
    为什么我点击linkLabel控件会完全没反应
      

  18.   

    呵呵
    你写的代码就是关闭整个程序的
    this.close()
      

  19.   

    Application.Exit(); 
    关闭所有了.
    this.close()
      

  20.   

    您都Application.Exit(); 
    了,当然都关闭了要用this.close();
      

  21.   

       用线程控制
         private void button1_Click(object sender, EventArgs e)
            {
                if (comboBox1.Text.Trim() == "") { MessageBox.Show("无效的用户名,请输入用户名!","提示",MessageBoxButtons.OK,MessageBoxIcon.Question); comboBox1.Focus(); return; }
                if (textBox1.Text == "") { MessageBox.Show("请输入你的密码后再登陆","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning); textBox1.Focus(); return; }
                string uname = Conn.ReturnString(Conn.GetDb, "select uname from t_user where uid='" + comboBox1.Text + "' and upassword ='" + textBox1.Text + "'");            if (uname.Trim()!="")
                {
                    Conn.loginname = uname;
                    Thread t = new Thread(new ThreadStart(OpenMain));
                    t.Start();
                }
                else
                {
                    MessageBox.Show("请输入你的用户名或密码错误,请重新确认", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    textBox1.Focus();
                    return;
                }
            }        private void button2_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
            //新增一个线程
            private void OpenMain()
            {
                FormMain main = new FormMain();
                main.WindowState = FormWindowState.Maximized;
                this.Close();
                main.ShowDialog();        }
      

  22.   

    晕,我也是和楼主一样的问题,后来去解决了:Program.cs 代码:using System;
    using System.Collections.Generic;
    using System.Windows.Forms;namespace Lay
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);      //frmLogin是主窗体
                frm = new frmLogin();
                while (frm != null)
                {
                    Application.Run(frm);
                    int nLayer = GC.GetGeneration(frm);
                    frm = null;
                    GC.Collect(nLayer);
                    frm = frmNext;
                    frmNext = null;            }            //frm2 = new frmInit();
                //while ((frm2 != null && frm==null) || frm2!=null)
                //{
                //    Application.Run(frm2);
                //    int nLayer2 = GC.GetGeneration(frm2);
                //    frm2 = null;
                //    GC.Collect(nLayer2);
                //    frm2 = frmNext2;
                //    frmNext2 = null;            //}
               
            }
            private static Form frm;
            public static Form frmNext;
            //private static Form frm2;
            //public static Form frmNext2;    }
    }
    使用:
    Program.frmNext = new frmInit();  //frmInit是你要打开的窗体
    this.Close();
      

  23.   

    01 static void Main()
    02 {
    03     Application.EnableVisualStyles();
    04     Application.SetCompatibleTextRenderingDefault(false);
    05 
    06     Application.Run(new LoginForm());              -- 这句放这里没有任何意义, LoginForm 为什么要有两个实例
    07     LoginForm lf = new LoginForm();
    08     if (lf.DialogResult == DialogResult.OK)        -- 这句应改为 lf.ShowDialog() == DialogResult.OK
    09     {
    10         Application.Run(new RegisterForm());
    11         lf.Close();                                -- 这句可以放在10行前面
    12     }
    13 }
      

  24.   

    使用this.hide()会不会产生其它影响,建议使用this.close();另外,在下想问的是this.hide()是否只是将窗体隐藏起来了,但是程序的运行却还没结束的呢?
      

  25.   

    你在LonginForm里形成Register的实例,然后调用this.Close();
    你在这里的this 不就代表LonginForm的实例吗!!!!!!!!!!!!!!!!!
      

  26.   

    if (comboBox1.Text.Trim() == "") { MessageBox.Show("无效的用户名,请输入用户名!","提示",MessageBoxButtons.OK,MessageBoxIcon.Question); comboBox1.Focus(); return; } 
                if (textBox1.Text == "") { MessageBox.Show("请输入你的密码后再登陆","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning); textBox1.Focus(); return; } 
                string uname = Conn.ReturnString(Conn.GetDb, "select uname from t_user where uid='" + comboBox1.Text + "' and upassword ='" + textBox1.Text + "'");             if (uname.Trim()!="") 
                { 
                    Conn.loginname = uname; 
                    Thread t = new Thread(new ThreadStart(OpenMain)); 
                    t.Start(); 
                } 
                else 
                { 
                    MessageBox.Show("请输入你的用户名或密码错误,请重新确认", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                    textBox1.Focus(); 
                    return; 
                } 
            }         private void button2_Click(object sender, EventArgs e) 
            { 
                Application.Exit(); 
            } 
            //新增一个线程 
            private void OpenMain() 
            { 
                FormMain main = new FormMain(); 
                main.WindowState = FormWindowState.Maximized; 
                this.Close(); 
                main.ShowDialog();         }