Stop a form from showing if an error occurs

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In the load event of our form, we load combos/textboxes from a database. If
the database is down or any other error, we want to display a message to the
user but not show that form at all. So, we're on one form and we click a
button to open this form. If an error, it just shows the error to the user.
The way we have it now, if an error occurs in the form, it just closes it.
That produces a flicker that we don't want.
 
You should handle this by making sure the forms initiailization is handled by
its constructor and that the form constructor either a) does not handle any
exceptions or b) that it rethrows the exception. Then if an error is thrown,
it will be caught before you ever call the forms Show method. This would be
the code on the calling form where you click a button:

private void cmdSomeButton_Click(object sender, System.EventArgs e)
{
try
{
SomeFormThatMightError frm = new SomeFormThatMightError();
frm.Show();
}
catch(System.Exception se)
{
MessageBox.Show(this, se.Message);
}
}

In this manner the form will throw its exception during initialization and
the .Show method of the form will never be called. you get your error
message and no "blink".

Hope that helps,

John Scragg
 
Back
Top