C# Opening a new form and closing the other one

前端 未结 5 499
深忆病人
深忆病人 2021-01-12 07:03

In my program I show a login form before the main form, once the program detects a successful login I use this:

MainForm Main = new MainForm();
Main.Show();
         


        
相关标签:
5条回答
  • 2021-01-12 07:44

    You can use ApplicationContext.MainForm to specify current main form for the application:

    static class Program
    {
        public static ApplicationContext Context { get; set; }
    
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
    
            Context = new ApplicationContext(new LoginForm());
            // pass Context instead of just new LoginForm()
            Application.Run(Context);
        }
    }
    

    then in in login handler:

    private void login_Click(object sender, EventArgs e)
    {
        Program.Context.MainForm = new MainForm();
    
        // close login form
        this.Close();
    
        // set up context to track main form instead of login
        Program.Context.MainForm.Show(); 
    }
    
    0 讨论(0)
  • 2021-01-12 07:47

    You need to specify your MainForm when you staring application and in the Load event handler of this form ask for login. In this case you will have runned application and Login for on the starting:

    Program.cs

        Application.Run(new MainForm());
    

    MainForm.cs

        private void Form1_Load(object sender, EventArgs e)
        {
            LoginForm loginForm = new LoginForm();
            if (loginForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // check login here
            }
        }
    

    P.S. Close will close your application completelly if it's main form of your application.

    0 讨论(0)
  • 2021-01-12 07:47

    How about this.Close() instead?

    0 讨论(0)
  • 2021-01-12 07:54

    Try something more like this:

    this.Hide();
    Main.ShowDialog();
    this.Close();
    

    You want to hide the login form before you show the dialog, then close the login form after the dialog has been closed.

    Simply closing the Login dialog will ultimately end the application, so that's not a real solution, but you still want to hide the login.

    Simply put, put things in the order you want them to go in, especially when dealing with message loops.

    First, you hide the login form.
    Next, you show the Main form dialog, but prevent the caller of "ShowDialog()" from continuing until the dialog is closed.
    Last, once the dialog is closed, you close the login form, ending the application.

    0 讨论(0)
  • 2021-01-12 08:02

    Change the main form to be MainForm, and when the application launches, in your MainForm_Load launch login form as a dialogbox, so they cannot access the main form.

    • If you need to be able to close the application from the login form, use Application.Exit(0);
    • If you don't want them to see the main form lookup and override SetVisibilityCore and call it inside MainForm_Load.
    0 讨论(0)
提交回复
热议问题