Closing a form programatically which was opened with ShowDIalog

I

Iain

I have a form (that I open with ShowDialog) which I wish to close either
with a button or through my program (after an event occurs in the main
program).

How do I do it?

Close doesn't work and I'm fairly sure that simply calling the click event
won't work.

It must be easy ....

Thanks


Iain
 
T

Tim Wilson

Try assigning a value to the DialogResult property of the Form displayed
using the ShowDialog method. Optionally, you can assign the appropriate
button objects to the Forms AcceptButton and CancelButton properties so that
when either of these two buttons is clicked the Form will close. But calling
the Close method of the Form shown using ShowDialog should close the Form.
 
I

Iain

Try assigning a value to the DialogResult property of the Form displayed
using the ShowDialog method. Optionally, you can assign the appropriate
button objects to the Forms AcceptButton and CancelButton properties so that
when either of these two buttons is clicked the Form will close. But calling
the Close method of the Form shown using ShowDialog should close the Form.

I've done the button thing and that works fine.

I've also read that Close does not close a form opened with ShowDialog().

Which matches my experience.

Since then I've opened it with Show(). However as soon as I close or hide
it it won're reshow (I actually create a new instance and show it. It pops
up momentarily and then hides itself again). ANy ideas on that one?

Iain
 
T

Tim Wilson

When a Form is shown using the ShowDialog method, it is not closed as
"expected". Instead it will be hidden so that it may be shown again. In
order to destroy a Form that has been shown using ShowDialog you should call
Dispose. So calling Close on a Form shown using ShowDialog will close the
Form is just won't destroy it. So I guess it all depends on how you
interpret the word "close" in this situation. The behavior is different when
a Form is shown using the Show method. See the remarks section at the link
below.
http://msdn.microsoft.com/library/d...stemwindowsformsformclassshowdialogtopic1.asp
 
I

Iain

When a Form is shown using the ShowDialog method, it is not closed as
"expected". Instead it will be hidden so that it may be shown again. In
order to destroy a Form that has been shown using ShowDialog you should call
Dispose. So calling Close on a Form shown using ShowDialog will close the
Form is just won't destroy it. So I guess it all depends on how you
interpret the word "close" in this situation. The behavior is different when
a Form is shown using the Show method. See the remarks section at the link
below.
http://msdn.microsoft.com/library/d...stemwindowsformsformclassshowdialogtopic1.asp

Beats me. I was actually seeing that close had no effect.

when I returned to ShowModal (with my 'close' being a hide followed by a
Dispose), it worked.

So I changed the hide back to a close and it worked again! Except that
every so often I get an exception caused by it trying to access a
'Disposed' button.

However it seems to work more or less OK and my originally observed
behaviour is now unreproduceable. Joy.

Thanks very much for your help!

Perplexed (as usual!)


Iain
 
T

Tim Wilson

When you only display a Form using ShowDialog (not Show) it shouldn't be
getting closed unless you are explicitly destroying it. What are you trying
to accomplish overall? It seems like you may be trying to access that Form,
or something on that Form, after it has been marked for disposal. The code
pattern that I usually follow when showing a dialog as modal is as follows.
Of course, this creates the dialog, and marks it for disposal when done,
everytime you need it.

Form2 form2Value = new Form2();
if (form2Value.ShowDialog() == DialogResult.OK)
{
// Do something if the user has "OK'd".
}
form2Value.Dispose();

If you don't care about the result then the code could look like this.

Form2 form2Value = new Form2();
form2Value.ShowDialog();
form2Value.Dispose();
 
I

Iain

When you only display a Form using ShowDialog (not Show) it shouldn't be
getting closed unless you are explicitly destroying it. What are you trying
to accomplish overall? It seems like you may be trying to access that Form,
or something on that Form, after it has been marked for disposal. The code
pattern that I usually follow when showing a dialog as modal is as follows.
Of course, this creates the dialog, and marks it for disposal when done,
everytime you need it.

What I want is to put up a modal dialog which the user may cancel.

However an external hardware operation (putting in a USB Drive in this
case) can also remove it from visibility.

So the dialog may say, 'Please put in the USB Drive or click cancel to
abort'.

Then they can click cancel or wait for ever or put in the USB drive (which
generates a WM_DEVICECHANGE message) and the system can then proceeed to
remove the dialog and carry on with its normal work.

I'm sure I've done it a dozen times in C++ and VB, just learning the new
paradigm, I guess.

An equivalent sort of functionality would be a dialog which disappears
after waiting 3 minutes for an input.

Hope this makes it clearer.


Iain
 
T

Tim Wilson

So when the WM_* message, that you may be looking for, is received, you
could always assign the DialogResult property of the modal Form with an
appropriate value and that should cause the dialog to hide and return the
DialogResult that you set. This may be useful so that you can handle the
difference between "something happening" and "the user saying cancel". So
there are a few different ways to close a Form. I hope that information
helps you in some small way.
 
I

Iain

So when the WM_* message, that you may be looking for, is received, you
could always assign the DialogResult property of the modal Form with an
appropriate value and that should cause the dialog to hide and return the
DialogResult that you set. This may be useful so that you can handle the
difference between "something happening" and "the user saying cancel". So
there are a few different ways to close a Form. I hope that information
helps you in some small way.

And it's even in the docs! Gah! Thank you.


Iain
 

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