469,317 Members | 1,701 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,317 developers. It's quick & easy.

Show() vs ShowDialog() Disposal Problem

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,
--
Randy
Jun 6 '06 #1
4 12027

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


randy1200 wrote: 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,

Jun 6 '06 #2

"Paul Cheetham" <PA******@dsl.pipex.com> wrote in message
news:e9**************@TK2MSFTNGP02.phx.gbl...

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


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

Jun 6 '06 #3
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!
--
Randy
"Mythran" wrote:

"Paul Cheetham" <PA******@dsl.pipex.com> wrote in message
news:e9**************@TK2MSFTNGP02.phx.gbl...

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


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

Jun 6 '06 #4
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!
--
Randy
"randy1200" wrote:
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!
--
Randy
"Mythran" wrote:

"Paul Cheetham" <PA******@dsl.pipex.com> wrote in message
news:e9**************@TK2MSFTNGP02.phx.gbl...

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


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

Jun 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Kostatus | last post: by
117 posts views Thread by Peter Olcott | last post: by
2 posts views Thread by JDR2000 | last post: by
20 posts views Thread by ed | last post: by
2 posts views Thread by zoneal | last post: by
7 posts views Thread by Steve | last post: by
10 posts views Thread by darkfact | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.