ShowDialog method

ShowDialog method

Post by Pete Marou » Sat, 22 Feb 2003 04:16:40



Hello all.  I am using the ShowDialog on a form (FormDialogBox).  The
form exposes a custom property (UserName).  I call the ShowDialog
method on the form, enter some data into the user name text box.
Press the OK button.  Then from the calling class (the parent), check
for DialogResult.OK.  If OK, then I display a MessageBox with the
UserName property.  After calling Dispose on the frmDialog, I make
another call to MessageBox.Show passing in the same UserName property.
This displays back the same name, even after I called Dispose()?  I
read that Close will not close because this was called via ShowDialog?
That's why I tried Dispose().  However, I have learned the a method
like Close() generally just calls Dispose?  Well, in either case, I am
getting back the same result, the value of the UserName property i
sbeing returned twice.  Can anyone explain this to me or point me to a
reference on this behavior?  

        FormDialogBox frmDialog = new FormDialogBox();
        if (frmDialog.ShowDialog() == DialogResult.OK)
        {
                MessageBox.Show("Thanks " + frmDialog.UserName);
                frmDialog.Dispose();
                MessageBox.Show("Thanks " + frmDialog.UserName);
        }

TIA

 
 
 

ShowDialog method

Post by Bob Powel » Sat, 22 Feb 2003 05:14:21


Dispose provides a mechanism for objects to free up resources that they may
use. It doesn't guarantee the immediate destruction of the object. Calling
dispose on a dialog won't kill it outright. Because it's still referred to
by the frmDialog variable the garbage collecter may still think its a needed
item.

If you do.. frmDialog=null and then GC.Collect() the thing will be well and
truly dead.

--

Check out the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm


Quote:> Hello all.  I am using the ShowDialog on a form (FormDialogBox).  The
> form exposes a custom property (UserName).  I call the ShowDialog
> method on the form, enter some data into the user name text box.
> Press the OK button.  Then from the calling class (the parent), check
> for DialogResult.OK.  If OK, then I display a MessageBox with the
> UserName property.  After calling Dispose on the frmDialog, I make
> another call to MessageBox.Show passing in the same UserName property.
> This displays back the same name, even after I called Dispose()?  I
> read that Close will not close because this was called via ShowDialog?
> That's why I tried Dispose().  However, I have learned the a method
> like Close() generally just calls Dispose?  Well, in either case, I am
> getting back the same result, the value of the UserName property i
> sbeing returned twice.  Can anyone explain this to me or point me to a
> reference on this behavior?

> FormDialogBox frmDialog = new FormDialogBox();
> if (frmDialog.ShowDialog() == DialogResult.OK)
> {
> MessageBox.Show("Thanks " + frmDialog.UserName);
> frmDialog.Dispose();
> MessageBox.Show("Thanks " + frmDialog.UserName);
> }

> TIA


 
 
 

ShowDialog method

Post by Brad Fewste » Sat, 22 Feb 2003 05:29:53


Just a shot in the dark here...

But what I've read about the GC is that it will only collect the object when
there are no remaining references to it.  So though you called dispose, the
GC wont collect it until you set your frmDialog to NULL (and of course the
GC runs). Until then, as long as you hang onto that frmDialog reference, the
object instance will never go away, and hence your member variable also will
not go away.

Thats my theory on it, anyone else? :)
B


Quote:> Hello all.  I am using the ShowDialog on a form (FormDialogBox).  The
> form exposes a custom property (UserName).  I call the ShowDialog
> method on the form, enter some data into the user name text box.
> Press the OK button.  Then from the calling class (the parent), check
> for DialogResult.OK.  If OK, then I display a MessageBox with the
> UserName property.  After calling Dispose on the frmDialog, I make
> another call to MessageBox.Show passing in the same UserName property.
> This displays back the same name, even after I called Dispose()?  I
> read that Close will not close because this was called via ShowDialog?
> That's why I tried Dispose().  However, I have learned the a method
> like Close() generally just calls Dispose?  Well, in either case, I am
> getting back the same result, the value of the UserName property i
> sbeing returned twice.  Can anyone explain this to me or point me to a
> reference on this behavior?

> FormDialogBox frmDialog = new FormDialogBox();
> if (frmDialog.ShowDialog() == DialogResult.OK)
> {
> MessageBox.Show("Thanks " + frmDialog.UserName);
> frmDialog.Dispose();
> MessageBox.Show("Thanks " + frmDialog.UserName);
> }

> TIA

 
 
 

ShowDialog method

Post by Brad Fewste » Sat, 22 Feb 2003 05:30:41


Doh... this message didnt show up in my newsreader.  Guess Bob agrees!

B


> Dispose provides a mechanism for objects to free up resources that they
may
> use. It doesn't guarantee the immediate destruction of the object. Calling
> dispose on a dialog won't kill it outright. Because it's still referred to
> by the frmDialog variable the garbage collecter may still think its a
needed
> item.

> If you do.. frmDialog=null and then GC.Collect() the thing will be well
and
> truly dead.

> --

> Check out the GDI+ FAQ
> http://www.bobpowell.net/gdiplus_faq.htm



> > Hello all.  I am using the ShowDialog on a form (FormDialogBox).  The
> > form exposes a custom property (UserName).  I call the ShowDialog
> > method on the form, enter some data into the user name text box.
> > Press the OK button.  Then from the calling class (the parent), check
> > for DialogResult.OK.  If OK, then I display a MessageBox with the
> > UserName property.  After calling Dispose on the frmDialog, I make
> > another call to MessageBox.Show passing in the same UserName property.
> > This displays back the same name, even after I called Dispose()?  I
> > read that Close will not close because this was called via ShowDialog?
> > That's why I tried Dispose().  However, I have learned the a method
> > like Close() generally just calls Dispose?  Well, in either case, I am
> > getting back the same result, the value of the UserName property i
> > sbeing returned twice.  Can anyone explain this to me or point me to a
> > reference on this behavior?

> > FormDialogBox frmDialog = new FormDialogBox();
> > if (frmDialog.ShowDialog() == DialogResult.OK)
> > {
> > MessageBox.Show("Thanks " + frmDialog.UserName);
> > frmDialog.Dispose();
> > MessageBox.Show("Thanks " + frmDialog.UserName);
> > }

> > TIA

 
 
 

1. show method and showdialog method

Sorry for such an easy thing but the basic form.show
method of displaying a form gives a non-shared member
error requiring an object reference.  All my past vb6
and .net books experience assume the form.show method is
correct.  Is my install wrong?  Can anybody help me not
feel so stupid?

2. Good book on Networking?

3. SEHException when using ShowDialog method

4. pointer to Spec Newsletter

5. Bug? Focus on topmost forms with ShowDialog() method

6. Sendmail 8.8.7 for SGI

7. Control.Invoke and Form.ShowDialog

8. CD/RW usefullness

9. bug with ShowDialog

10. form.ShowDialog()

11. form.ShowDialog() : Help

12. Form.ShowDialog problems

13. Showdialog throws Null Ref Exception