473,652 Members | 2,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12373

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.p ipex.com> wrote in message
news:e9******** ******@TK2MSFTN GP02.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_Closin g 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.p ipex.com> wrote in message
news:e9******** ******@TK2MSFTN GP02.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_FormCl osing(object sender, FormClosingEven tArgs 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_Closin g 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.p ipex.com> wrote in message
news:e9******** ******@TK2MSFTN GP02.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
3749
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 (ClassB* b; b->func(); ) the base-class function is called instead of the new function in the derived class. All other similar functions (virtual in the base class and overwritten in the the derived class) work fine, it's just this one function. ...
117
7175
by: Peter Olcott | last post by:
www.halting-problem.com
2
1156
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 before the form is painted. Is there an event that indicates that the form has been completely displayed on the screen, so I can then begin the sound player? Many thanks in advance,
20
2432
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
1060
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 listbox xhanges(nothing wrong with that), then I have assigned cases to the diffrent rows of the listbox for when I klick on 1 row it will show diffrent things in the richtextbox, but the problem is I dont know how to show diffrent cases for...
7
2629
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 sender, EventArgs e) { frmImportData ImportData = new frmImportData();
3
1954
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 here it is. Seems to have something to do with doing ShowDialog Easy to reproduce this project, simply add two forms.
3
7292
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 ID="btnClickMe" Text="Click Me...!" OnclientClick="HideDiv('PostNewTopic')" runat="Server" /> JavaScript Code is:
10
2257
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(){ document.getElementById("test").style.visibility="hidden"
0
8283
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8704
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8590
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4147
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2707
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.