How to prevent a form from closing - 2 part question

G

Guest

Hi,

I am trying to prevent a form from being closed by the user until the code
in the OnLoad method executes. This code can take quite some time to
complete executing. This form itself is launched from my main form using
ShowDialog.

I cannot use an approach like having another form showing 'Please Wait' or
something to that effect.

The basic problem here is that I need to run some code on the Close event of
the form, which does not get executed if the user closes the form as I
described.

I guess another way of asking my question is whether there is any way the
code in the Close event will ALWAYS get executed - some other event etc?
 
D

Daniel Carlsson

Heres a (not tested) idea, main thing is that you can cancel the closing of
a form in its Closing event.

Sub OnLoad(...)
Me.Enabled = False
...
Me.Enabled = True
End Sub

Sub OnClosing(...)
e.Cancel = Not Me.Enabled
End Sub

Hope it helps
/Dan
 
A

Armin Zingler

Dilip M said:
Hi,

I am trying to prevent a form from being closed by the user until the
code in the OnLoad method executes. This code can take quite some
time to complete executing. This form itself is launched from my
main form using ShowDialog.

I cannot use an approach like having another form showing 'Please
Wait' or something to that effect.

The basic problem here is that I need to run some code on the Close
event of the form, which does not get executed if the user closes
the form as I described.

I guess another way of asking my question is whether there is any way
the code in the Close event will ALWAYS get executed - some other
event etc?

I don't see how it can be possible for the user to close the Form *before*
it has been loaded.

Armin
 
G

Guest

I don't think I explained it clearly...the form closing event does NOT fire.
That is the root of my problem, since the code that I need executed is in
that method.

The loading of the form takes quite some time, and the user may close it
before the loading has completed.
 
G

Guest

The code in the constructor and the OnLoad method take quite a bit of time to
execute. So it is possbile to see the form while it is being painted; at
this time the user may close it.
 
D

Daniel Carlsson

Oh sorry, I misread.

How about doing the loading asynchronous instead then? Just disable the
elements that wont work because its still loading and use Form.Invoke to
inform the form of when it can enable the elements and load them from the
data computed? That should solve your problem, although watch out for having
the form being disposed by the time you reach the Form.Invoke call.

/Dan
 
A

Armin Zingler

Dilip M said:
The code in the constructor and the OnLoad method take quite a bit of
time to execute. So it is possbile to see the form while it is
being painted; at this time the user may close it.

In OnLoad, a form is not visible yet unless you make it visible there, but
that's considered to be bad design.

Armin
 

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