Results 1 to 4 of 4

Thread: Form Show - Disposing Form

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,746

    Form Show - Disposing Form

    Stemming from this conversation: https://www.vbforums.com/showthread....=1#post5638114

    I was told that when you call the Show method of a form that there is no need to dispose of the form because the disposal is actually handled for you when it closes. When asking for the documentation, it looks like everything pointed to the Close method. But I don't get it because doing something like this:
    Code:
    Dim frm = New Form2()
    frm.Show()
    And allowing the user to click on the close button (top-right red "x" button in windows) means that I never called the Close method. Looking through the Form's Reference Source, I don't see where the Close method is being called.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: Form Show - Disposing Form

    Out of curiosity I put a messagebox in the form closing event, it was trigger when I clicked on the "x"

    It was also triggered when ShowDialog was used.

    Also the Closed event is trigger. I don't know is the closed Method is called.
    Last edited by wes4dbt; Apr 2nd, 2024 at 04:17 PM.

  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,746

    Re: Form Show - Disposing Form

    OK, I think I see it now. It looks like it is calling Dispose in the WM_Close (reference) when this condition is true:
    Code:
    if (m.Msg != NativeMethods.WM_QUERYENDSESSION) {
        // ...
        OnClosed(fc);
        OnFormClosed(fc);
     
        Dispose();
    }
    Can someone verify? I'm not up on the WM_* stuff.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,343

    Re: Form Show - Disposing Form

    I'll do some testing myself too. Maybe I was making an incorrect assumption. A dialogue is dismissed when you set the DialogResult property so maybe it is only in that case that the form needs to be disposed separately. Maybe actually calling Close disposes the form either way. I know that a form displayed with Show is disposed when it's closed and a form displayed with ShowDialog is not disposed when you set its DialogResult. The question is whether a form displayed with ShowDialog is also disposed when you call Close.

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