Results 1 to 7 of 7

Thread: me.close vs me.dispose

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    263

    me.close vs me.dispose

    I find that if I do me.close and then reopen the form with showDialog later, things are as they were before I closed it. If I use me.dispose and then later open the form again, its as if it was never opened before.

    Are these two both valid methods for leaving a form?

    Thanks
    Dave

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: me.close vs me.dispose

    I suggest that you read the documentation for the Form.Close method. It explains when closing a form disposes it and when it doesn't.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: me.close vs me.dispose

    Form.Close Method
    Quote Originally Posted by MSDN
    The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    263

    Re: me.close vs me.dispose

    I see. I thought you were supposed to show new forms with ShowDialog? It seems like that's not the case since me.close doesn't work with ShowDialog?

    Dave

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: me.close vs me.dispose

    ShowDialog won't let you switch to other forms while you are showing a form using this method, there is also the Show method. When using ShowDialog all the codes that follows it until the the form is not closed. You can use Me.Close in the form shown using ShowDialog, why did you say that it doesn't work?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: me.close vs me.dispose

    Quote Originally Posted by daviddoria
    I see. I thought you were supposed to show new forms with ShowDialog? It seems like that's not the case since me.close doesn't work with ShowDialog?

    Dave
    ShowDialog displays a form as a modal dialogue. If you want your form displayed as a modal dialogue then you're supposed to display it with ShowDialog. If you don't want your form displayed as a modal dialogue then you're not supposed to display it with ShowDialog.

    Of course Me.Close works with modal dialogues. It does exactly what it's supposed to do: it closes the form. It doesn't dispose it because it's not supposed to dispose it. Calling Me.Close in a modal dialogue, i.e. a form that has been displayed with ShowDialog, is equivalent to setting the DialogResult property to Cancel: the form is dismissed and ShowDialog returns Cancel.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: me.close vs me.dispose

    A good way to make sure that you are disposing a form shown in dialog mode is to use the following code structure:

    Code:
    Dim result As DialogResult
    
    Using frm1 As New Form1
        result = frm1.ShowDialog()
    End Using
    The using statement is very good because it will handle the disposal of any object that implements the IDisposable interface, which all forms do by default.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width