472,353 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 12263

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the...
117
by: Peter Olcott | last post by:
www.halting-problem.com
2
by: JDR2000 | last post by:
I am creating a form, then displaying it using ShowDialog(). The problem is that thie form is supposed to display itself, and then a sound is...
20
by: ed | last post by:
Hi I can't seem to make the Me.Show() work My code is dim dlgresult as dialogresul dlgresult=me.show( The message is : "expression does...
2
by: zoneal | last post by:
Ok I got 3 problems and I aint very good at this, but here goes. First problem: I got an application with an listbox and a richtextbox and...
7
by: Steve | last post by:
I have a method that creates of new form each time its called do i need to dipose of the form when it closes or doe the garbage collector take care...
3
by: Franky | last post by:
Been having a problem using a treeview. Work great the first time the form containing it is entered but not the second time. Took a while to...
3
by: vinitfichadia | last post by:
Hi friend, I've been using server control button, and on its click event i m showing or hiding the div content on the basis of its current style....
10
by: darkfact | last post by:
I'm using this code to show and hide a div. function showDiv(){ document.getElementById("test").style.visibility="visible" } function...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.