0

I have Forms (Admin, login, user). There is no error when accessing user form, it is simply opening it without user and pass.

The error I encountered is when I login and Admin form is present, when I click in my toolstripbutton the whole application exits.

This is the method I applied in every toolstripbutton:

public void CloseAllActiveForms()
        {
            List<Form> openForms = new List<Form>();

            foreach (Form f in Application.OpenForms)
                openForms.Add(f);

            foreach (Form f in openForms)
            {
                if (f.Name != "FrmAdmin")
                {
                    f.Close();

                }
            }
        }

And this is the code in every toolstipbutton:

private void toolStripButton4_Click(object sender, EventArgs e)
        {
            CloseAllActiveForms();
            FrmDashboard objFORM = new FrmDashboard();
            objFORM.MdiParent = this;
            objFORM.TopLevel = false;
            objFORM.FormBorderStyle = FormBorderStyle.None;
            objFORM.Dock = DockStyle.Fill;
            pnlMain.Controls.Add(objFORM);
            objFORM.Show();
        }

This is design sample.

enter image description here

Every click in toolstripbutton supposed to go in pnlMain, but the problem occurs that after logging in and click one of the toolstripbutton it quits the whole windows application.

I tried to search about these and I found almost the same as my problem but the solution I think is not for my problem because I think it is for two forms only and the main form will be put in program.cs, but i have 2 and I think but not sure that my main form is the login. Please enlighten me.

Thank you

Patrick
  • 5,526
  • 14
  • 64
  • 101
Peri Forus
  • 19
  • 1
  • 6

1 Answers1

0

Because of this line in your program.cs

Application.Run(new FrmLogin());

the form FrmLogin is the main form of your application.

When the main form of an application is closed, the application is terminated. I suppose that after Login, you do not close but only hide the login form.

However, your code in CloseAllActiveForms will also close the hidden login form.

A quick solution is probably to skip all hidden forms in CloseAllActiveForms or explicitly skip FrmLogin in CloseAllActiveForms.

public void CloseAllActiveForms()
{
    List<Form> openForms = new List<Form>();

    foreach (Form f in Application.OpenForms)
        openForms.Add(f);

    foreach (Form f in openForms)
    {
        if (f.Visible && !(f is FrmAdmin) && !(f is FrmLogin))
        {
            f.Close();

        }
    }
}

There are alternative cleaner solutions:

  1. Show the login form from the main form. See how to show login form on front & Mainform Behind Login Form?

  2. Pass an ApplicationContext instead of a Form to Application.Run. This allows you to have more control over when the application is closed. See Windows Forms: create the main application after login, which form to run?

NineBerry
  • 26,306
  • 3
  • 62
  • 93