Close a MDI Child form during load?

T

Tom

Is there ANY easy way to close a MDI Child form in the middle of it's load?
For instance, during the Load event I find a need to close the form (for
whatever reason - maybe the user isn't ready for it yet, or it displays
another form and they hit cancel). In any event, I need to be able to cause
that MDI Child form to close. Just sticking Me.Close in there won't work,
generates an error (note however, that on a NON-MDI form this seems to work
fine - go figure). I thought I could 'catch' the error in an error block
from the main MDI form; that does work - however, it must leave something
open because trying to close the main MDI form after this by using the X
button in the title bar no longer works.

Any suggestions?

Tom
 
H

Herfried K. Wagner [MVP]

* "Tom said:
Is there ANY easy way to close a MDI Child form in the middle of it's load?

No. You can throw an exception to close the form, but I am sure that
there is a better solution.
 
T

Tom

What event, if any, runs AFTER the load is complete? If I can find something
there maybe I can set up variable to trigger a close in that event, once the
load is complete.

Tom
 
T

Tom

I figured out one way, but it has to be the STUPIDEST way I have ever seen!

1. Put a timer on the form and set the timer to 100 and disabled
2. When you need to close the form in the load event:
a. Move the form off the screen (me.left=-20000)
b. Enable the timer
c. Exit the load sub
3. In the timer Tick event, close the form (i.e. Me.Close)

You will get a quick flash of the scroll bars as the window is moved off the
boundries of the form and closed, but it does work.

There has GOT to be a better way to close an MDI child form during the
load!! What happens if you find an error in the load (like if reading a
database and no records show up) but you DON'T want the whole application to
shut down - just to show an error message and then close the MDI child form?
Again, there has to be an easier or cleaner method than the above way.

Anyone got ANY ideas?

Tom
 
A

AlexS

Tom,

I would suggest to reconsider. Suppose user opens MDI child and Load
encounters errors. You close form and don't give a chance to user to see
what was the reason. Possibly cleaner solution would be to open form and
display it's status - valid information error or whatever. Form can invoke
event in parent window to pass back fail / success indicator too. And main
window might close it without such hacks. Otherwise you might incite user to
click and click, until he/she figures that something is wrong and complains
about your application.

HTH
Alex

Tom said:
I figured out one way, but it has to be the STUPIDEST way I have ever seen!

1. Put a timer on the form and set the timer to 100 and disabled
2. When you need to close the form in the load event:
a. Move the form off the screen (me.left=-20000)
b. Enable the timer
c. Exit the load sub
3. In the timer Tick event, close the form (i.e. Me.Close)

You will get a quick flash of the scroll bars as the window is moved off the
boundries of the form and closed, but it does work.

There has GOT to be a better way to close an MDI child form during the
load!! What happens if you find an error in the load (like if reading a
database and no records show up) but you DON'T want the whole application to
shut down - just to show an error message and then close the MDI child form?
Again, there has to be an easier or cleaner method than the above way.

Anyone got ANY ideas?

Tom
 
H

Herfried K. Wagner [MVP]

* "Tom said:
What event, if any, runs AFTER the load is complete? If I can find something
there maybe I can set up variable to trigger a close in that event, once the
load is complete.

Why do you show the form? Why display it if there is a problem? Add
the code to the form's constructor ('Sub New') and throw an exception
there to cancel construction of the instance.
 
A

Armin Zingler

Tom said:
Is there ANY easy way to close a MDI Child form in the middle of it's
load? For instance, during the Load event I find a need to close the
form (for whatever reason - maybe the user isn't ready for it yet, or
it displays another form and they hit cancel). In any event, I need
to be able to cause that MDI Child form to close. Just sticking
Me.Close in there won't work, generates an error (note however, that
on a NON-MDI form this seems to work fine - go figure). I thought I
could 'catch' the error in an error block from the main MDI form;
that does work - however, it must leave something open because trying
to close the main MDI form after this by using the X button in the
title bar no longer works.

Any suggestions?


Move the code out of Form_Load and execute it before showing the form. Show
the Form only if the code was successful.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 

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