Aborting form load

  • Thread starter Thread starter James Bender
  • Start date Start date
J

James Bender

Hi,

I have a form that checks some things if its load event. If something
happens I would like to form handle it gracefully, which means
closing. How can I close a form from the load event? I tried
this.Close(), but I get a run-time error.

Thanks,
James
 
benderj1 said:
Hi,

I have a form that checks some things if its load event. If something
happens I would like to form handle it gracefully, which means
closing. How can I close a form from the load event? I tried
this.Close(), but I get a run-time error.

Check for these "things" before you even load the form:

static void Main()
{
// check special things
// and exit if not met.

Application.Run(new Form1());
}
 
That had occured to me. The problem is that this form is part of a
library which is consumed from several sources. I would really like to
keep the logic encapsulated in the form, or failing that, the library. I
am considering changing the form from public to internal and creating a
new public object which the consumers will have to go through, but I
wanted to first see if their was an easier way to go about it.

Thanks,
James
 
James said:
That had occured to me. The problem is that this form is part of a
library which is consumed from several sources. I would really like to
keep the logic encapsulated in the form, or failing that, the library. I
am considering changing the form from public to internal and creating a
new public object which the consumers will have to go through, but I
wanted to first see if their was an easier way to go about it.

There is. Have the logic in the form, in a method called ReadyToLoad()
that returns a Boolean. The caller can then instantiate the form, ask
whether's it ready to load, and if so, call the Show/Load/etc. method.
 
James,
One of the easier ways to abort the Form.Load event is to throw an Exception
in the Form.Load event.

Of course if you are handling the Application.ThreadException then it gets a
little trickier...

Hope this helps
Jay
 
Back
Top