Closing forms and controls

Using VS2005, windows forms, c#

I have 6 forms, some of them children, some have user controls on them.

In the main form I check that a file exists, if not then I want to close the application.

Using Application.Exit(); doesn't seem to work.

What is the best way to close everything and exit



Answer this question

Closing forms and controls

  • mahima

    Hmm, odd. Does one of those 6 forms implement the FormClosing event and cancels it


  • razored

    No in fact no other forms are loaded at this point.

    this is my code:

    public frmMain()

    {

    InitializeComponent();

    //Set all the global variables

    this.fnSetGlobal();

    }

    #region Form //------------------------------------------------------

    private void frmMain_Load(object sender, EventArgs e)

    {

    //Show the Developer name

    this.tsDeveloper.Text = GlobalVars.GV._Developer;

    //Show the Version

    this.tsVersion.Text = GlobalVars.GV._Version;

    //Create a new instance of the Login Form

    frmLogIn chFormD = new frmLogIn(this );

    //Display the child Login

    chFormD.ShowDialog(this);

    //Set the header of this form as choosen in the Login Form

    switch (GlobalVars.GV._SelectedCompany)

    {

    case "1":

    this.tsCompany.Text = GlobalVars.GV._CompanyName1;

    break;

    case "2":

    this.tsCompany.Text = GlobalVars.GV._CompanyName2;

    break;

    }

    }

    #endregion Form

    #region Control-Q //-------------------------------------------------------

    private void QuotestoolStripMenuItem1_Click(object sender, EventArgs e)

    {

    switch (GlobalVars.GV._SelectedCompany)

    {

    case "1":

    //Create a new instance of the MDI child Quotations

    frmQuotations chFormQ = new frmQuotations();

    //Display the child window

    chFormQ.ShowDialog(this);

    break;

    case "2":

    //To Do

    break;

    }

    }

    private void AbouttoolStripMenuItem2_Click(object sender, EventArgs e)

    {

    //Create a new instance of the MDI child Quotations

    frmAbout chFormA = new frmAbout();

    //Display the about form

    chFormA.ShowDialog();

    }

    #endregion Control-Q //-------------------------------------------------------

    private void fnSetGlobal()

    {

    //Set the global variables

    }

    else

    {

    MessageBox.Show("Cannot connect to config");

    Application.Exit();

    }

    }


  • Suresh Venkatraman

    You're calling Application.Exit() in the form constructor; that won't work because the application hasn't started yet. Put the call to fnSetGlobal() in the Load event and it will work.



  • Closing forms and controls