Groups | Blog | Home
all groups > dotnet windows forms > may 2005 >

dotnet windows forms : How to prevent a form from closing - 2 part question


Daniel Carlsson
5/13/2005 12:00:00 AM
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

[quoted text, click to view]

Armin Zingler
5/13/2005 12:00:00 AM
"Dilip M" <Dilip M@discussions.microsoft.com> schrieb
[quoted text, click to view]

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

Armin
Dilip M
5/13/2005 2:41:23 AM
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
Dilip M
5/13/2005 5:03:03 AM
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.

[quoted text, click to view]
Dilip M
5/13/2005 5:04:01 AM
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.

[quoted text, click to view]
Daniel Carlsson
5/13/2005 3:27:57 PM
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

[quoted text, click to view]

Armin Zingler
5/13/2005 3:43:03 PM
"Dilip M" <DilipM@discussions.microsoft.com> schrieb
[quoted text, click to view]

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

Armin
AddThis Social Bookmark Button