Programmatically Closing form on load

J

jgc

How do I programmatically close a form after I do some
initial processing? Form.Close() doesn't work in the
constructor or in the form's Load() event.

I've also tried creating an event handler to call a
function that then runs Form.Close(), but that doesn't
work. Has anyone tried closing (without user
intervention) a form after it has just started? Thanks.

jgc
 
H

Herfried K. Wagner [MVP]

jgc said:
How do I programmatically close a form after I do some
initial processing? Form.Close() doesn't work in the
constructor or in the form's Load() event.

I've also tried creating an event handler to call a
function that then runs Form.Close(), but that doesn't
work. Has anyone tried closing (without user
intervention) a form after it has just started?

Throw an exception.
 
J

jgc

Thanks Herfried, but I just tried throwing an exception
and it didn't work -- it continued execution of commands
even after the call to Close() in the catch.
Adding "return" doesn't help either.

Any other suggestions would be appreciated. Thanks.

try { throw new System::ArgumentException(); }
catch (System::ArgumentException*)
{
Close();
return;
}
 
H

Herfried K. Wagner [MVP]

jgc said:
Thanks Herfried, but I just tried throwing an exception
and it didn't work -- it continued execution of commands
even after the call to Close() in the catch.
Adding "return" doesn't help either.

Any other suggestions would be appreciated. Thanks.

try { throw new System::ArgumentException(); }
catch (System::ArgumentException*)
{
Close();
return;
}

Throw the exception in the form's constructor and do _not_ catch it inside
the form. You can catch it when calling the form's 'Show' or 'ShowDialog'
method.
 
S

Stephany Young

Why an earth have you put a try on a throw?

If you want it to happen in the constructor then throw the exception in your
form constructor.

If you want it to happen in the form Load event then throw the exception in
a handler for MyBase.Load.

Now, where you invoke your form, put the try/catch:

Try
Dim f As New <form> ' Line 1
f.Show ' Line 2
Catch ex As ArgumentException
' Whatever you want to do
End Try

If the exception is thrown in the constructor it will happen at line 1, if
it is thrown in the Load event handler it will happen at line 2.


Thanks Herfried, but I just tried throwing an exception
and it didn't work -- it continued execution of commands
even after the call to Close() in the catch.
Adding "return" doesn't help either.

Any other suggestions would be appreciated. Thanks.

try { throw new System::ArgumentException(); }
catch (System::ArgumentException*)
{
Close();
return;
}
 

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