Disposing of Windows Forms

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Henry Jones

    #1

    Disposing of Windows Forms

    I have a project that has 5 or 6 forms.
    VB.NET VS 2005


    In the FormClosing Event of each form I have the following code:

    If Not IsNothing(frmA) Or Not frmA.IsDisposed Then frmA.Close()

    If Not IsNothing(frmB) Or Not frmB.IsDisposed Then frmB.Close()

    If Not IsNothing(frmC) Or Not frmC.IsDisposed Then frmC.Close()

    If Not IsNothing(frmD) Or Not frmD.IsDisposed Then frmD.Close()

    Me.Dispose()



    In Form B I have to remove the: If Not IsNothing(frmB) Or Not
    frmB.IsDisposed Then frmB.Close()

    In Form C I have to remove the: If Not IsNothing(frmC) Or Not
    frmC.IsDisposed Then frmC.Close()



    etc.... Is there a way to iterate through all the forms in the FormClosing
    event to make sure everything is closed before ending the application?



    Thanks


  • RobinS

    #2
    Re: Disposing of Windows Forms

    What are you trying to accomplish?

    Are you calling these in the respective forms, i.e. you're doing "If Not
    IsNothing(frmA) ..." in frmA, and "If Not IsNothing(frmB) ..." in frmB and so
    on, just to close the forms? Because you don't need to.

    Presumably if you are *in* the FormClosing event, someone has chosen to
    close the form, and it is being closed. At that point, asking to close it
    would be repetitive.

    Unlike older versions of VB, you no longer have to set the form to Nothing
    in order to let the system know it is no longer needed. VB2005 marks it as
    "unused" when there are no longer any references to it, and the
    GarbageCollecto r gets rid of it on its next sweep through. This happens to
    most objects, be it a form or a variable or a class object, whenever it goes
    out of scope and there are no references to it.

    Hope that helps.
    Robin

    "Henry Jones" <henry@yada.com wrote in message
    news:eg%23H71s$ GHA.3396@TK2MSF TNGP02.phx.gbl. ..
    >I have a project that has 5 or 6 forms.
    VB.NET VS 2005
    >
    >
    In the FormClosing Event of each form I have the following code:
    >
    If Not IsNothing(frmA) Or Not frmA.IsDisposed Then frmA.Close()
    >
    If Not IsNothing(frmB) Or Not frmB.IsDisposed Then frmB.Close()
    >
    If Not IsNothing(frmC) Or Not frmC.IsDisposed Then frmC.Close()
    >
    If Not IsNothing(frmD) Or Not frmD.IsDisposed Then frmD.Close()
    >
    Me.Dispose()
    >
    >
    >
    In Form B I have to remove the: If Not IsNothing(frmB) Or Not
    frmB.IsDisposed Then frmB.Close()
    >
    In Form C I have to remove the: If Not IsNothing(frmC) Or Not
    frmC.IsDisposed Then frmC.Close()
    >
    >
    >
    etc.... Is there a way to iterate through all the forms in the
    FormClosing event to make sure everything is closed before ending the
    application?
    >
    >
    >
    Thanks
    >
    >

    Comment

    • Henry Jones

      #3
      Re: Disposing of Windows Forms

      Yes, I am calling these respective forms in each form. In FormA I am
      calling the FormB, FormC, FormD statements
      In FormB I am Calling A,C,D, etc... So what you are saying is that I don't
      need to call these when I am closing down the Program?

      All that work for nothing :-)


      "RobinS" <RobinS@NoSpam. yah.nonewrote in message
      news:2aWdndQBbK ky7tfYnZ2dnUVZ_ qqdnZ2d@comcast .com...
      What are you trying to accomplish?
      >
      Are you calling these in the respective forms, i.e. you're doing "If Not
      IsNothing(frmA) ..." in frmA, and "If Not IsNothing(frmB) ..." in frmB and
      so on, just to close the forms? Because you don't need to.
      >
      Presumably if you are *in* the FormClosing event, someone has chosen to
      close the form, and it is being closed. At that point, asking to close it
      would be repetitive.
      >
      Unlike older versions of VB, you no longer have to set the form to Nothing
      in order to let the system know it is no longer needed. VB2005 marks it as
      "unused" when there are no longer any references to it, and the
      GarbageCollecto r gets rid of it on its next sweep through. This happens to
      most objects, be it a form or a variable or a class object, whenever it
      goes out of scope and there are no references to it.
      >
      Hope that helps.
      Robin
      >
      "Henry Jones" <henry@yada.com wrote in message
      news:eg%23H71s$ GHA.3396@TK2MSF TNGP02.phx.gbl. ..
      >>I have a project that has 5 or 6 forms.
      >VB.NET VS 2005
      >>
      >>
      >In the FormClosing Event of each form I have the following code:
      >>
      >If Not IsNothing(frmA) Or Not frmA.IsDisposed Then frmA.Close()
      >>
      >If Not IsNothing(frmB) Or Not frmB.IsDisposed Then frmB.Close()
      >>
      >If Not IsNothing(frmC) Or Not frmC.IsDisposed Then frmC.Close()
      >>
      >If Not IsNothing(frmD) Or Not frmD.IsDisposed Then frmD.Close()
      >>
      >Me.Dispose()
      >>
      >>
      >>
      >In Form B I have to remove the: If Not IsNothing(frmB) Or Not
      >frmB.IsDispose d Then frmB.Close()
      >>
      >In Form C I have to remove the: If Not IsNothing(frmC) Or Not
      >frmC.IsDispose d Then frmC.Close()
      >>
      >>
      >>
      >etc.... Is there a way to iterate through all the forms in the
      >FormClosing event to make sure everything is closed before ending the
      >application?
      >>
      >>
      >>
      >Thanks
      >>
      >>
      >
      >

      Comment

      • RobinS

        #4
        Re: Disposing of Windows Forms

        No effort is useless if you learn something from it. (Now lift your feet,
        it's getting deep in here.)

        Generally, you might have a main form, and have it close everything before
        it exits, just to make sure you didn't leave anything open. But you
        shouldn't close each form in its own close event.

        I have a main form (MainForm - how imaginative), and I might check and close
        all other forms like the following. This is assuming you don't want to
        finish closing the main form until all the other ones are closed.

        For Each myForm as Form in My.Application. OpenForms
        'MainForm is closing, so don't fire the close event again
        if myForm.Name <"MainForm" Then
        myForm.Close
        End If
        Next
        'do other stuff before finally letting MainForm close

        If your app starts up with a Sub Main(), you would put the following code
        there. If you wanted to put it in every form, or you didn't care what order
        they were closed, you could also do the following, but put it in the
        FormClosed event rather than FormClosing, though, because FormClosed fires
        AFTER the form is closed.

        For Each myForm as Form in My.Application. OpenForms
        myForm.Close
        Next

        By the way, if those are child forms of an MDI parent, the parent closes
        them when the parent is closed.

        Hope that gives you some closure. (Sorry; couldn't resist.)
        Robin

        "Henry Jones" <henryjones@yad a.comwrote in message
        news:%236pkOLu$ GHA.4060@TK2MSF TNGP03.phx.gbl. ..
        Yes, I am calling these respective forms in each form. In FormA I am
        calling the FormB, FormC, FormD statements
        In FormB I am Calling A,C,D, etc... So what you are saying is that I
        don't need to call these when I am closing down the Program?
        >
        All that work for nothing :-)
        >
        >
        "RobinS" <RobinS@NoSpam. yah.nonewrote in message
        news:2aWdndQBbK ky7tfYnZ2dnUVZ_ qqdnZ2d@comcast .com...
        >What are you trying to accomplish?
        >>
        >Are you calling these in the respective forms, i.e. you're doing "If Not
        >IsNothing(frmA )..." in frmA, and "If Not IsNothing(frmB) ..." in frmB and
        >so on, just to close the forms? Because you don't need to.
        >>
        >Presumably if you are *in* the FormClosing event, someone has chosen to
        >close the form, and it is being closed. At that point, asking to close it
        >would be repetitive.
        >>
        >Unlike older versions of VB, you no longer have to set the form to
        >Nothing in order to let the system know it is no longer needed. VB2005
        >marks it as "unused" when there are no longer any references to it, and
        >the GarbageCollecto r gets rid of it on its next sweep through. This
        >happens to most objects, be it a form or a variable or a class object,
        >whenever it goes out of scope and there are no references to it.
        >>
        >Hope that helps.
        >Robin
        >>
        >"Henry Jones" <henry@yada.com wrote in message
        >news:eg%23H71s $GHA.3396@TK2MS FTNGP02.phx.gbl ...
        >>>I have a project that has 5 or 6 forms.
        >>VB.NET VS 2005
        >>>
        >>>
        >>In the FormClosing Event of each form I have the following code:
        >>>
        >>If Not IsNothing(frmA) Or Not frmA.IsDisposed Then frmA.Close()
        >>>
        >>If Not IsNothing(frmB) Or Not frmB.IsDisposed Then frmB.Close()
        >>>
        >>If Not IsNothing(frmC) Or Not frmC.IsDisposed Then frmC.Close()
        >>>
        >>If Not IsNothing(frmD) Or Not frmD.IsDisposed Then frmD.Close()
        >>>
        >>Me.Dispose( )
        >>>
        >>>
        >>>
        >>In Form B I have to remove the: If Not IsNothing(frmB) Or Not
        >>frmB.IsDispos ed Then frmB.Close()
        >>>
        >>In Form C I have to remove the: If Not IsNothing(frmC) Or Not
        >>frmC.IsDispos ed Then frmC.Close()
        >>>
        >>>
        >>>
        >>etc.... Is there a way to iterate through all the forms in the
        >>FormClosing event to make sure everything is closed before ending the
        >>application ?
        >>>
        >>>
        >>>
        >>Thanks
        >>>
        >>>
        >>
        >>
        >
        >

        Comment

        Working...