Show() vs ShowDialog() Disposal Problem

G

Guest

I have a SideForm. If the use presses a button on the main form, the side
form comes up by executing the following:

SideForm sf = new SideForm()
sf.ShowDialog();

This works perfectly.

I've been asked to change the code to the following:

SideForm sf = new SideForm();
sf.Show();

The idea is that the user should not be excluded from using the main form
while the side form is visible. This works fine, except if the user closes
the side form and clicks the button re-open the side form. On re-open, I get
the following exception:

Cannot access a disposed object.
Object name: 'SideForm'

I've tried to play games with disposing and deleting the object. What I'd
really like to know is how to invoke the behavior of ShowDialog(), so that I
don't have dispose of every "new"ed third party control control on the side
form (there's a lot of them).

Thanks,
 
P

Paul Cheetham

Hi,

How about not disposing of your side form, but just hiding it?
Make a private or protected reference in your main for to your side
form. When the user clicks the button, check if you reference is null,
and if so create the side form, and assign it to your reference.
Then show it.
When the user closes the side form, just hide it instead.

e.g.

protected SideForm _sf;
In button click

if (sf == null)
sf = new SideForm();

sf.Show()



Paul
 
M

Mythran

Paul Cheetham said:
Hi,

How about not disposing of your side form, but just hiding it?
Make a private or protected reference in your main for to your side form.
When the user clicks the button, check if you reference is null, and if so
create the side form, and assign it to your reference.
Then show it.
When the user closes the side form, just hide it instead.

e.g.

protected SideForm _sf;


if (sf == null)
sf = new SideForm();

sf.Show()



Paul

To further build from this example, I would do something more along the
lines of...

protected SideForm _sf;

protected SideForm SideForm
{
get {
if (_sf == null) {
_sf = new SideForm();
}
return _sf;
}
}

Then, all calls to SideForm would return a reference, and you don't have to
check for null manually in every spot you want to get the var from. :)

HTH,
Mythran
 
G

Guest

Thanks for the responses. The problem is that the SideForm dialog box display
the Minimize, Maximize, Close buttons in the upper-right-most corner. When
the user clicks the close button, the sf object is disposed. If I try to
reuse or sf variable created by the new SideForm() statement, I get the
exception about trying to use a disposed object.

I guess the question I really should be asking is: How do I find the
SideForm_Closing event. I select the SideForm dialog box and click the
lighting bolt to see available events. I don't see Closing. If I could add
e.Cancel = true; to the closing event, I could stop Dispose from running!
 
G

Guest

Again, many thanks for the responses. I did find the closing event. I should
have looked a little closer.

To summarize:
In the SideForm class, I added the following. This stops dispose from
running, and yet allows the SideForm dialog to disappear. Now, if the user
re-opens the SideForm, sf is preserved and the SideForm is in the exact state
the user left it in. This is perfect.

private void SideForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Hide();
}


To actually kill the SideForm from the MainForm:

if (cfn != null)
cfn.Dispose();

Thanks again!
 

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