Aborting form load

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
 
P

Patrick Steele [MVP]

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());
}
 
J

James Bender

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
 
K

Kevin P. Fleming

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.
 
J

Jay B. Harlow [MVP - Outlook]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top