Programmatically Closing form on load

  • Thread starter Thread starter jgc
  • Start date Start date
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
 
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.
 
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;
}
 
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.
 
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;
}
 
Back
Top