Exit an Application

  • Thread starter Thread starter Rajesh Abraham
  • Start date Start date
R

Rajesh Abraham

I have a form, which is getting some settings from the
App.config in the form load. If the Key doesn't exist, I
need to close the application after trapping the error.

I tried Application.Exit() and Form.Close() neither seems
to be the right one.

Thanks,

Rajesh Abraham Chacko
 
Rajesh,

The location of the Application.Exit() is crucial in this working. Can you
provide more information about just what functions perform your config read,
and how you are trying to trap this...perhaps provide a complete code sample
that fails?
 
Rajesh,

The location of the Application.Exit() is crucial in this working. Can you
provide more information about just what functions perform your config read,
and how you are trying to trap this...perhaps provide a complete code sample
that fails?
 
Thanks for the response. Below is the code from a Helper
function that is called from the Form Load. The Helper is
just getting the DefaultSchoolName from the App.config. If
it could not get the value then Combos
cmbSchool.SelectedIndex will be -1, in which case I would
like to terminate the Application

Thanks,

Rajesh Abraham Chacko

-----------------------Code Below--------------

System.Configuration.AppSettingsReader appReader =new
System.Configuration.AppSettingsReader();
string defaultSchoolName=
appReader.GetValue "ImportToAd.DefaultSchoolName",typeof
(System.String)).ToString();
cmbSchool.SelectedIndex=cmbSchool.FindStringExact
(defaultSchoolName);

if (cmbSchool.SelectedIndex== -1)
{
MessageBox.Show("Failed to Get the Default School Name");
this.Close();
}
 
If you call this.Close() or Application.Exit() on a form in the contructor,
it won't close. If this form is the main form in your application, trying
loading your config settings in your Main() function, before you call
Application.Run(). Then, if you can't find the key, just don't call
Application.Run() and let your program finish before the form is ever
instantiated.

Chris
 
Back
Top