Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my Program.cs :
C#
static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Login login = new Login();
            Application.Run(login);
            if (login.LoginSuccess == true)
            {
                Application.Run(new Form_Main());
                
            }
        }

After successfully login it opens main form (Form_Main.cs). Now I want to add FormClosing event to Run Login form again after closing Form_Main.

In my Form_Main.cs :
C#
public Form_Main()
        {
            InitializeComponent();
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form_Main_FormClosing);
        }
private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr = MessageBox.Show("Do you REALLY want to close this app?", "Closing event!", MessageBoxButtons.YesNo);
            if (dr == DialogResult.No)
            
                e.Cancel = true;
            

            else
            {
                e.Cancel = false;
                Application.Run(new Login());            
                  
            }
        }

This is not working. Actually in here I want to close form named Form_Main and want to show another form named Login. Please, help me in FormClosing event to do that.

Thanks in advance.

What I have tried:

How to open Login Form in Closing event of Main Form while click in close button ?
Posted
Updated 7-Mar-16 0:13am

1 solution

Don't.
When the Main form closes, the Application.Run in your Main method will return. At that point you can loop round to do your login code again. But...make sure you provide some method (other than Task Manager) to let the user terminate the whole application, or they will get very annoyed with you when it's time to go home and turn the PC off...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900