Hi everyone,
What is the best method [pattern] to close a newly opened Windows form in C#/.NET if there is a trappable error in the CTOR/_Load event ?
i.e.,
bool loadError;
MyForm_Load(...) {
try {
} catch (SomeException ex) {
// Alert the user
MessageBox.Show("There was a critical error. The form will close. Please try again.");
// There was some sort of error, and I just want the form to close now.
loadError = true;
return;
}
}
Now I want to act on loadError. I've tired using the Activate event, but that yielded no results. Any suggestions on what the best way to go about doing this is ?
Update: Perhaps I should have been a little more explicit. Doing a "this.Close();" during the forms Load event will cause an exception to be thrown as a form can't be closed and the exception will be "Cannot call Close() while doing CreateHandle()".