VB.Net >> ShowDialog() =?= Dispose()?

by Phill. W » Thu, 01 Jul 2004 23:42:28 GMT

Under what circumstances could a Form, displayed modally using
.ShowDialog, be instantly and untrappably Dispose'd??

I've built a MsgBox replacement but, in one application, it gets
called with the contents of an Exception and, instead of ShowDialog()
popping up the dialog, buttons and all, it "sort of" puts up the "outline"
of the dialog, but immediately Dispose's it - dialog.IsDisposed = True
immediately after the .ShowDialog call.

In another application, however, it works perfectly.

/Any/ Suggestions?

Phill W.

VB.Net >> ShowDialog() =?= Dispose()?

by hirf-spam-me-here » Fri, 02 Jul 2004 00:09:02 GMT

* "Phill. W" < XXXX@XXXXX.COM > scripsit:

Post your code.

Herfried K. Wagner [MVP]
<URL: http://dotnet.mvps.org/> ;

VB.Net >> ShowDialog() =?= Dispose()?

by Phill. W » Fri, 02 Jul 2004 18:26:35 GMT



I've found and fixed the problem, but I'm not entirely sure I
understand just /why/ it's happening in the first place.

My dialog was being shown from the Click event handler on a
[Close] button in an /inherited/ form. The base form, of course :-/
- had code in the ancestor [Close] button to Close() the base form.
So, by the time my derived Close button Click-hander was
invoked and wanted to show my dialog, the base form had been
Close()'d and Dispose()'d.

All that I can understand - /now/ - but the bit that's got me stumped
is this; my dialog resizes itself based on the text it has to display, so
it uses Graphics.MeasureString() on the "prompt" value I give it.
Fine, except that I have to get a Graphics object from somewhere so,
within my "MsgBox" Form, I'm using

Dim g As System.Drawing.Graphics _
= System.Drawing.Graphics.FromHwnd(Me.Handle)

If the form I'm launching my dialog from has already been Disposed(),
this line throws an ObjectDisposedException, but "Me" /hasn't/ been
Dispose()'d - the form /from which/ I'm launching this [dialog] form has!
The "MsgBox" form is not inherited from (nor otherwise related to) the
form that's trying to ShowDialog() it (indeed, I replaced my dialog with
a "New Form" and got exactly the same symptoms.

Is there some implicit realtionship between the [Window handle of a]
form that launches a dialog and that of the dialog itself? Or, more
likely, is the above code the /wrong/ way to get hold of a Form's
graphics object?

Phill W.

Similar Threads

1. ApplicationEx.ShowDialog and Form.Dispose

First off, I want to say a big thank you to all the guys that created
the OpenNET SDF--even though I'm not using the entire library directly
in my app, I've found much of it to be extremely useful and helpful,
and I'm using several bits of source code in my project directly.

Recently, I've started using ApplicationEx and the IMessageFilter
interface so that I can prefilter windows messages (which works
wonderfully, by the way!).  The problem I am running in to is the use
of ApplicationEx.ShowDialog().  This method disposes of the dialog form
when it's done with it.  However, I don't want it to do this--I keep a
reference to my dialog around and show it again when the user clicks
the button again (besides, as I understand it, Form.ShowDialog() on
both the desktop and compact framework doesn't dispose the form).  I
added a boolean flag parameter to ApplicationEx.ShowDialog so that I
could prevent it from disposing the form.  However, when the user hits
the button and I call ApplicationEx.ShowDialog again, I get an
ObjectDisposedException.  Specifically, this happens on Form.Show().  I
put a breakpoint in my dialog form's Dispose method, but it is never
being reached.

What could be causing the ObjectDisposedException when my form's
dispose is never being called?  Is it possible to change
ApplicationEx.ShowDialog to allow it to be called multiple times on the
same form?


2. Problem after Disposing a ShowDialog() Form

3. Tooltip, .ShowDialog(), and .Dispose() on .NET 2.0 generate a NullReferenceException


I have the following problem, most probably a bug:

I have a windows form that incorporates one Button control and one ToolTip 

On the form's Load event, I am setting the tooltip message of the button by 
calling the SetTooltip() method.
On the button's Click event the form is disposed using the .Dispose() 

If the form is opened from another form using the .ShowDialog() method, if 
the button is pressed I get a NullReferenceException.

    1. If the form is called using the .Show() method then the exception 
does not occur.
    2. If the SetTooltip() method is not called then the exception does not 
    3. If the .Close() method is used on the Click event, instead of the 
Dispose(), and the .Dispose method is called imediatelly after the 
.ShowDialog() method, the exception does not occur.
    4. This problem did not occur on the .NET 1.1

Is this some kind of bug ?


4. Calling Dispose() after ShowDialog() - .NET Windows Forms

5. GDI resource leaks: Calling Dispose after ShowDialog

I am fighting exhausted resources in a large application with many 
forms. It manifests itself by EVENTUALLY not being able to load an 
ImageList. In researching this I see advice in previous postings to call 
Dispose on a form when finished with the object, after a ShowDialog. It 
is suggested this is necessary to avoid GDI resource leaks.

My question is, what about when you open the form non-modally with Show? 
Will the form do its own Dispose, or am I obligated to find a way for 
this to happen? If so, can someone suggest a strategy for managing this?

- Lee

6. does ShowDialog automatically dispose? - .NET Windows Forms

7. ShowDialog, Show and Disposed


I'm working on a program that will have 3 forms: a main form, a status
form and a sub-form.

The main form and status form are always appear, and my sub-form is a
singleton (for performance issue).

As usual, I have two ways to display the sub-form: Show() and
When I use ShowDialog() (the main form calls it), the program works
fine but I cannot access the main form and the status form (also
called by the main form). Because I still want to have access to the
other forms, I have to use Show()

But my program will crash with Show() because unlike ShowDialog(),
when the form that opened with Show() is closed, the form is also
disposed. This is not what I wanted because it's a singleton and I
want it to be disposed when the main form closed (the whole program

I think I should override an event to suspent the Disposing but I
can't find which event I should use, and how would I do it. Any help
is welcome.

Best Regards,
Homa Wong

8. Dispose, finalize, ~Class, & protected void Dispose(bool disposing)