473,387 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 12356

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 function using a pointer to the derived class...
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 played. Unfortunately, the sound always starts up...
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 not produce a value
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 three buttons, when I press the diffrent button the...
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 of it? code private void button1_Click(object...
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 create a small sample that exhibits the problem, but...
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. ASP code for button is: <asp:Button...
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 hideDiv(){...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.