How to clear controls from a form - Help Needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?U2l2?=

    How to clear controls from a form - Help Needed

    I have a form that I programmaticall y generate some check boxes and labels on.
    Later on when I want to draw the form with different data I want to clear
    the previously created items and then put some new ones on.

    In my code I am doing the following:

    For Each ctrl In tpMain.Controls
    If TypeOf (ctrl) Is CheckBox Then
    If ctrl.Name.Start sWith("chkS") Then
    ctrl.Visible = False
    ctrl.Dispose()
    End If
    End If
    End if
    Next

    What I am finding however is that in some cases controls that I know have
    been affected by the above loop are staying on the form and not being made
    invisible and then being disposed.

    Is there a sure fire method of removing the controls rather than the dispose
    method which presumably is not always removing the control for whatever
    reason??

    Siv
    --
    Martley, Near Worcester, UK
  • Rich P

    #2
    Re: How to clear controls from a form - Help Needed

    It would be much easier/simpler/reliable to have an extra form with the
    extra controls. When a user needs to use the other set of controls -
    instead of creating a whole new set of controls just bring up the extra
    form and hide the main form - or if it even matters don't do anything
    with the main form. Just leave it running.

    Rich

    *** Sent via Developersdex http://www.developersdex.com ***

    Comment

    • =?Utf-8?B?U2l2?=

      #3
      Re: How to clear controls from a form - Help Needed

      I can't do that because the amount of check boxes differs dramatically
      between redraws, there isn't a fixed layout. On one set of data I may only
      need to show 2 check boxes on another ther may be 20 or 30. I am trying to
      avoid the overhead of having thousands of controls on the forma nd only
      create them as I need them and then when I have finished with them destroy
      them again.

      The problem i think is tha the dispose method doesn't always decide to
      dispose when I want it to and is probably waiting for the system to quieten
      down before actioning the request.

      I really need a Ctrl.delete type comand.

      Siv
      --
      Martley, Near Worcester, UK


      "Rich P" wrote:
      It would be much easier/simpler/reliable to have an extra form with the
      extra controls. When a user needs to use the other set of controls -
      instead of creating a whole new set of controls just bring up the extra
      form and hide the main form - or if it even matters don't do anything
      with the main form. Just leave it running.
      >
      Rich
      >
      *** Sent via Developersdex http://www.developersdex.com ***
      >

      Comment

      • Lloyd Sheen

        #4
        Re: How to clear controls from a form - Help Needed

        Siv wrote:
        I can't do that because the amount of check boxes differs dramatically
        between redraws, there isn't a fixed layout. On one set of data I may only
        need to show 2 check boxes on another ther may be 20 or 30. I am trying to
        avoid the overhead of having thousands of controls on the forma nd only
        create them as I need them and then when I have finished with them destroy
        them again.
        >
        The problem i think is tha the dispose method doesn't always decide to
        dispose when I want it to and is probably waiting for the system to quieten
        down before actioning the request.
        >
        I really need a Ctrl.delete type comand.
        >
        Siv
        Try this:

        Create a panel onto which you will place your dynamic controls.

        When you are finished with those controls, you should have a variable
        for the original panel. Use the Clear method to get rid of all the
        controls you created dynamically.

        You now should have a blank panel upon which you can add controls with
        new info.

        Hope this helps
        LS

        Comment

        • Stephany Young

          #5
          Re: How to clear controls from a form - Help Needed

          For _i = tpMain.Controls .Count - 1 To 0 Step -1
          Dim _ctrl = tpMain.Controls (_i)
          If TypeOf (_ctrl) Is CheckBox AndAlso _ctrl.Name.Star tsWith("chkS") Then
          tpMain.Controls .RemoveAt(_i)
          _ctrl.Dispose()
          End If
          Next


          "Siv" <Siv@discussion s.microsoft.com wrote in message
          news:ECC5102D-8DAC-4D39-B504-0F4E8BD8A6F8@mi crosoft.com...
          >I have a form that I programmaticall y generate some check boxes and labels
          >on.
          Later on when I want to draw the form with different data I want to clear
          the previously created items and then put some new ones on.
          >
          In my code I am doing the following:
          >
          For Each ctrl In tpMain.Controls
          If TypeOf (ctrl) Is CheckBox Then
          If ctrl.Name.Start sWith("chkS") Then
          ctrl.Visible = False
          ctrl.Dispose()
          End If
          End If
          End if
          Next
          >
          What I am finding however is that in some cases controls that I know have
          been affected by the above loop are staying on the form and not being made
          invisible and then being disposed.
          >
          Is there a sure fire method of removing the controls rather than the
          dispose
          method which presumably is not always removing the control for whatever
          reason??
          >
          Siv
          --
          Martley, Near Worcester, UK

          Comment

          • =?Utf-8?B?U2l2?=

            #6
            Re: How to clear controls from a form - Help Needed

            Lloyd,
            Thanks for your response, these controls are already on a TabPage control
            called tpMain.

            When you say perform a clear, is that a method of the panel itself. Will
            that clear all controls? There are a number of controls which are placed on
            the TabPage at design time, would they be destroyed as well?

            Interesteing idea though, I could certainly add a panel onto the TabPage in
            the area where I want to display the check boxes and only add the controls
            that are dynamically created to that panel.

            Cheer,

            Siv
            --
            Martley, Near Worcester, UK


            "Lloyd Sheen" wrote:
            Siv wrote:
            I can't do that because the amount of check boxes differs dramatically
            between redraws, there isn't a fixed layout. On one set of data I may only
            need to show 2 check boxes on another ther may be 20 or 30. I am trying to
            avoid the overhead of having thousands of controls on the forma nd only
            create them as I need them and then when I have finished with them destroy
            them again.

            The problem i think is tha the dispose method doesn't always decide to
            dispose when I want it to and is probably waiting for the system to quieten
            down before actioning the request.

            I really need a Ctrl.delete type comand.

            Siv
            >
            Try this:
            >
            Create a panel onto which you will place your dynamic controls.
            >
            When you are finished with those controls, you should have a variable
            for the original panel. Use the Clear method to get rid of all the
            controls you created dynamically.
            >
            You now should have a blank panel upon which you can add controls with
            new info.
            >
            Hope this helps
            LS
            >

            Comment

            • =?Utf-8?B?U2l2?=

              #7
              Re: How to clear controls from a form - Help Needed

              Stephany,
              Thanks for your reply. Can you explain why the additional command

              tpMain.Controls .RemoveAt(_i)

              will do the trick where just doing the dispose on its own does not?

              I appreciate you gave me the answer, but I try to learn from my questions
              here. Don't worry if you are too busy to reply, but if you do have time I
              would appreciate it.

              Siv
              --
              Martley, Near Worcester, UK


              "Stephany Young" wrote:
              For _i = tpMain.Controls .Count - 1 To 0 Step -1
              Dim _ctrl = tpMain.Controls (_i)
              If TypeOf (_ctrl) Is CheckBox AndAlso _ctrl.Name.Star tsWith("chkS") Then
              tpMain.Controls .RemoveAt(_i)
              _ctrl.Dispose()
              End If
              Next
              >
              >
              "Siv" <Siv@discussion s.microsoft.com wrote in message
              news:ECC5102D-8DAC-4D39-B504-0F4E8BD8A6F8@mi crosoft.com...
              I have a form that I programmaticall y generate some check boxes and labels
              on.
              Later on when I want to draw the form with different data I want to clear
              the previously created items and then put some new ones on.

              In my code I am doing the following:

              For Each ctrl In tpMain.Controls
              If TypeOf (ctrl) Is CheckBox Then
              If ctrl.Name.Start sWith("chkS") Then
              ctrl.Visible = False
              ctrl.Dispose()
              End If
              End If
              End if
              Next

              What I am finding however is that in some cases controls that I know have
              been affected by the above loop are staying on the form and not being made
              invisible and then being disposed.

              Is there a sure fire method of removing the controls rather than the
              dispose
              method which presumably is not always removing the control for whatever
              reason??

              Siv
              --
              Martley, Near Worcester, UK
              >
              >

              Comment

              • Stephany Young

                #8
                Re: How to clear controls from a form - Help Needed

                Your problem was that, although you were 'disposing' the control, you were
                not removing it from tpMain's collection of controls. Therefore you end up
                with 'phantom' controls in tpMain's control collection.

                Others might argue to the contrary, but I believe that the Dispose method of
                a CheckBox control does nothing special, and it certainly will not remove
                itself from it's parent collection.

                Therefore the code can be further simplified to:

                For _i = tpMain.Controls .Count - 1 To 0 Step -1
                Dim _ctrl = tpMain.Controls (_i)
                If TypeOf (_ctrl) Is CheckBox AndAlso _ctrl.Name.Star tsWith("chkS") Then
                tpMain.Controls .RemoveAt(_i)
                Next

                To save you asking, the reverse loop is to ensure that iterating on the
                control collection is still viable even when one or more of the iteration
                actually removes an element.



                "Siv" <Siv@discussion s.microsoft.com wrote in message
                news:3A906198-C470-43E1-9A2D-3A3E2CDF834E@mi crosoft.com...
                Stephany,
                Thanks for your reply. Can you explain why the additional command
                >
                tpMain.Controls .RemoveAt(_i)
                >
                will do the trick where just doing the dispose on its own does not?
                >
                I appreciate you gave me the answer, but I try to learn from my questions
                here. Don't worry if you are too busy to reply, but if you do have time I
                would appreciate it.
                >
                Siv
                --
                Martley, Near Worcester, UK
                >
                >
                "Stephany Young" wrote:
                >
                >For _i = tpMain.Controls .Count - 1 To 0 Step -1
                > Dim _ctrl = tpMain.Controls (_i)
                > If TypeOf (_ctrl) Is CheckBox AndAlso _ctrl.Name.Star tsWith("chkS")
                >Then
                > tpMain.Controls .RemoveAt(_i)
                > _ctrl.Dispose()
                > End If
                >Next
                >>
                >>
                >"Siv" <Siv@discussion s.microsoft.com wrote in message
                >news:ECC5102 D-8DAC-4D39-B504-0F4E8BD8A6F8@mi crosoft.com...
                >I have a form that I programmaticall y generate some check boxes and
                >labels
                >on.
                Later on when I want to draw the form with different data I want to
                clear
                the previously created items and then put some new ones on.
                >
                In my code I am doing the following:
                >
                For Each ctrl In tpMain.Controls
                If TypeOf (ctrl) Is CheckBox Then
                If ctrl.Name.Start sWith("chkS") Then
                ctrl.Visible = False
                ctrl.Dispose()
                End If
                End If
                End if
                Next
                >
                What I am finding however is that in some cases controls that I know
                have
                been affected by the above loop are staying on the form and not being
                made
                invisible and then being disposed.
                >
                Is there a sure fire method of removing the controls rather than the
                dispose
                method which presumably is not always removing the control for whatever
                reason??
                >
                Siv
                --
                Martley, Near Worcester, UK
                >>
                >>

                Comment

                • =?Utf-8?B?U2l2?=

                  #9
                  Re: How to clear controls from a form - Help Needed

                  Stephany,

                  Thanks for clarifying, clearly I thought that to dispose of a control would
                  by inference remove it from the tpMain collection of controls. It's the old
                  story "never assume".

                  Thanks for your help.

                  Siv
                  --
                  Martley, Near Worcester, UK


                  "Stephany Young" wrote:
                  Your problem was that, although you were 'disposing' the control, you were
                  not removing it from tpMain's collection of controls. Therefore you end up
                  with 'phantom' controls in tpMain's control collection.
                  >
                  Others might argue to the contrary, but I believe that the Dispose method of
                  a CheckBox control does nothing special, and it certainly will not remove
                  itself from it's parent collection.
                  >
                  Therefore the code can be further simplified to:
                  >
                  For _i = tpMain.Controls .Count - 1 To 0 Step -1
                  Dim _ctrl = tpMain.Controls (_i)
                  If TypeOf (_ctrl) Is CheckBox AndAlso _ctrl.Name.Star tsWith("chkS") Then
                  tpMain.Controls .RemoveAt(_i)
                  Next
                  >
                  To save you asking, the reverse loop is to ensure that iterating on the
                  control collection is still viable even when one or more of the iteration
                  actually removes an element.
                  >
                  >
                  >
                  "Siv" <Siv@discussion s.microsoft.com wrote in message
                  news:3A906198-C470-43E1-9A2D-3A3E2CDF834E@mi crosoft.com...
                  Stephany,
                  Thanks for your reply. Can you explain why the additional command

                  tpMain.Controls .RemoveAt(_i)

                  will do the trick where just doing the dispose on its own does not?

                  I appreciate you gave me the answer, but I try to learn from my questions
                  here. Don't worry if you are too busy to reply, but if you do have time I
                  would appreciate it.

                  Siv
                  --
                  Martley, Near Worcester, UK


                  "Stephany Young" wrote:
                  For _i = tpMain.Controls .Count - 1 To 0 Step -1
                  Dim _ctrl = tpMain.Controls (_i)
                  If TypeOf (_ctrl) Is CheckBox AndAlso _ctrl.Name.Star tsWith("chkS")
                  Then
                  tpMain.Controls .RemoveAt(_i)
                  _ctrl.Dispose()
                  End If
                  Next
                  >
                  >
                  "Siv" <Siv@discussion s.microsoft.com wrote in message
                  news:ECC5102D-8DAC-4D39-B504-0F4E8BD8A6F8@mi crosoft.com...
                  I have a form that I programmaticall y generate some check boxes and
                  labels
                  on.
                  Later on when I want to draw the form with different data I want to
                  clear
                  the previously created items and then put some new ones on.

                  In my code I am doing the following:

                  For Each ctrl In tpMain.Controls
                  If TypeOf (ctrl) Is CheckBox Then
                  If ctrl.Name.Start sWith("chkS") Then
                  ctrl.Visible = False
                  ctrl.Dispose()
                  End If
                  End If
                  End if
                  Next

                  What I am finding however is that in some cases controls that I know
                  have
                  been affected by the above loop are staying on the form and not being
                  made
                  invisible and then being disposed.

                  Is there a sure fire method of removing the controls rather than the
                  dispose
                  method which presumably is not always removing the control for whatever
                  reason??

                  Siv
                  --
                  Martley, Near Worcester, UK
                  >
                  >
                  >
                  >

                  Comment

                  • Lloyd Sheen

                    #10
                    Re: How to clear controls from a form - Help Needed

                    Siv wrote:
                    Lloyd,
                    Thanks for your response, these controls are already on a TabPage control
                    called tpMain.
                    >
                    When you say perform a clear, is that a method of the panel itself. Will
                    that clear all controls? There are a number of controls which are placed on
                    the TabPage at design time, would they be destroyed as well?
                    >
                    Interesteing idea though, I could certainly add a panel onto the TabPage in
                    the area where I want to display the check boxes and only add the controls
                    that are dynamically created to that panel.
                    >
                    Cheer,
                    >
                    Siv
                    Clear will clear all controls on the container (panel) so you would
                    loose other "non-dynamic" controls. Putting a panel on the tab page
                    would certainly work.

                    LS

                    Comment

                    • =?Utf-8?B?U2l2?=

                      #11
                      Re: How to clear controls from a form - Help Needed

                      Lloyd,
                      I am just trying Stephany's method as it involves less changes than yours,
                      but if her method doesn't solve teh problem I'll redesign the form slightly
                      and implement your idea.

                      Thanks again for your help.

                      Siv
                      --
                      Martley, Near Worcester, UK


                      "Lloyd Sheen" wrote:
                      Siv wrote:
                      Lloyd,
                      Thanks for your response, these controls are already on a TabPage control
                      called tpMain.

                      When you say perform a clear, is that a method of the panel itself. Will
                      that clear all controls? There are a number of controls which are placed on
                      the TabPage at design time, would they be destroyed as well?

                      Interesteing idea though, I could certainly add a panel onto the TabPage in
                      the area where I want to display the check boxes and only add the controls
                      that are dynamically created to that panel.

                      Cheer,

                      Siv
                      >
                      Clear will clear all controls on the container (panel) so you would
                      loose other "non-dynamic" controls. Putting a panel on the tab page
                      would certainly work.
                      >
                      LS
                      >

                      Comment

                      • Cor Ligthert[MVP]

                        #12
                        Re: How to clear controls from a form - Help Needed

                        Siv,

                        Disposing is not a removing method or whateverf, it is just telling the GC
                        that it "can be" ready to clear up, like the GC does that automaticly when
                        it is really needed. Dispose is meant to put it at the garbage. As anology,
                        some people are the whole day busy with cleaning up there desktop (putting
                        it in the trash bin, which still will not be direct emptied), others do that
                        once a day or/and some when that is needed.

                        As it is about forms, you can see in your designer part of the class
                        (versions before 2005 normal in the class) the implementing of IDisposable
                        of a form.

                        As long as an object is referenced by something (like on a form with
                        controls.add) it will not be cleared as long as that has a reference,
                        whatever other method you call (including dispose), mostly is that by clear
                        them all in one time or by removing them one by one.

                        Cor


                        "Siv" <Siv@discussion s.microsoft.com schreef in bericht
                        news:EBBE4C00-2842-475E-97E5-01F9D7491B53@mi crosoft.com...
                        Stephany,
                        >
                        Thanks for clarifying, clearly I thought that to dispose of a control
                        would
                        by inference remove it from the tpMain collection of controls. It's the
                        old
                        story "never assume".
                        >
                        Thanks for your help.
                        >
                        Siv
                        --
                        Martley, Near Worcester, UK
                        >
                        >
                        "Stephany Young" wrote:
                        >
                        >Your problem was that, although you were 'disposing' the control, you
                        >were
                        >not removing it from tpMain's collection of controls. Therefore you end
                        >up
                        >with 'phantom' controls in tpMain's control collection.
                        >>
                        >Others might argue to the contrary, but I believe that the Dispose method
                        >of
                        >a CheckBox control does nothing special, and it certainly will not remove
                        >itself from it's parent collection.
                        >>
                        >Therefore the code can be further simplified to:
                        >>
                        > For _i = tpMain.Controls .Count - 1 To 0 Step -1
                        > Dim _ctrl = tpMain.Controls (_i)
                        > If TypeOf (_ctrl) Is CheckBox AndAlso _ctrl.Name.Star tsWith("chkS")
                        >Then
                        >tpMain.Control s.RemoveAt(_i)
                        > Next
                        >>
                        >To save you asking, the reverse loop is to ensure that iterating on the
                        >control collection is still viable even when one or more of the iteration
                        >actually removes an element.
                        >>
                        >>
                        >>
                        >"Siv" <Siv@discussion s.microsoft.com wrote in message
                        >news:3A90619 8-C470-43E1-9A2D-3A3E2CDF834E@mi crosoft.com...
                        Stephany,
                        Thanks for your reply. Can you explain why the additional command
                        >
                        tpMain.Controls .RemoveAt(_i)
                        >
                        will do the trick where just doing the dispose on its own does not?
                        >
                        I appreciate you gave me the answer, but I try to learn from my
                        questions
                        here. Don't worry if you are too busy to reply, but if you do have time
                        I
                        would appreciate it.
                        >
                        Siv
                        --
                        Martley, Near Worcester, UK
                        >
                        >
                        "Stephany Young" wrote:
                        >
                        >For _i = tpMain.Controls .Count - 1 To 0 Step -1
                        > Dim _ctrl = tpMain.Controls (_i)
                        > If TypeOf (_ctrl) Is CheckBox AndAlso _ctrl.Name.Star tsWith("chkS")
                        >Then
                        > tpMain.Controls .RemoveAt(_i)
                        > _ctrl.Dispose()
                        > End If
                        >Next
                        >>
                        >>
                        >"Siv" <Siv@discussion s.microsoft.com wrote in message
                        >news:ECC5102 D-8DAC-4D39-B504-0F4E8BD8A6F8@mi crosoft.com...
                        >I have a form that I programmaticall y generate some check boxes and
                        >labels
                        >on.
                        Later on when I want to draw the form with different data I want to
                        clear
                        the previously created items and then put some new ones on.
                        >
                        In my code I am doing the following:
                        >
                        For Each ctrl In tpMain.Controls
                        If TypeOf (ctrl) Is CheckBox Then
                        If ctrl.Name.Start sWith("chkS") Then
                        ctrl.Visible = False
                        ctrl.Dispose()
                        End If
                        End If
                        End if
                        Next
                        >
                        What I am finding however is that in some cases controls that I know
                        have
                        been affected by the above loop are staying on the form and not
                        being
                        made
                        invisible and then being disposed.
                        >
                        Is there a sure fire method of removing the controls rather than the
                        dispose
                        method which presumably is not always removing the control for
                        whatever
                        reason??
                        >
                        Siv
                        --
                        Martley, Near Worcester, UK
                        >>
                        >>
                        >>
                        >>

                        Comment

                        • Jack Jackson

                          #13
                          Re: How to clear controls from a form - Help Needed

                          Cor,

                          I'm sure it is useless to argue with you, but it is misleading to say
                          that the purpose of Dispose is to tell the GC that the object " 'can
                          be' ready to clear up".

                          The purpose if Dispose is to release unmanaged resources. For
                          Controls, that would mostly be the Windows handle for the associated
                          Windows control. If you fail to call Dispose for a Control that has
                          allocated a Windows handle, then the handle might no be freed for a
                          very long time, and if you do this enough times you might run out of
                          Windows handles.

                          On Tue, 5 Aug 2008 06:47:35 +0200, "Cor Ligthert[MVP]"
                          <notmyfirstname @planet.nlwrote :
                          >Siv,
                          >
                          >Disposing is not a removing method or whateverf, it is just telling the GC
                          >that it "can be" ready to clear up, like the GC does that automaticly when
                          >it is really needed. Dispose is meant to put it at the garbage. As anology,
                          >some people are the whole day busy with cleaning up there desktop (putting
                          >it in the trash bin, which still will not be direct emptied), others do that
                          >once a day or/and some when that is needed.
                          >
                          >As it is about forms, you can see in your designer part of the class
                          >(versions before 2005 normal in the class) the implementing of IDisposable
                          >of a form.
                          >
                          >As long as an object is referenced by something (like on a form with
                          >controls.add ) it will not be cleared as long as that has a reference,
                          >whatever other method you call (including dispose), mostly is that by clear
                          >them all in one time or by removing them one by one.
                          >
                          >Cor
                          >
                          >
                          >"Siv" <Siv@discussion s.microsoft.com schreef in bericht
                          >news:EBBE4C0 0-2842-475E-97E5-01F9D7491B53@mi crosoft.com...
                          >Stephany,
                          >>
                          >Thanks for clarifying, clearly I thought that to dispose of a control
                          >would
                          >by inference remove it from the tpMain collection of controls. It's the
                          >old
                          >story "never assume".
                          >>
                          >Thanks for your help.
                          >>
                          >Siv
                          >--
                          >Martley, Near Worcester, UK
                          >>
                          >>
                          >"Stephany Young" wrote:
                          >>
                          >>Your problem was that, although you were 'disposing' the control, you
                          >>were
                          >>not removing it from tpMain's collection of controls. Therefore you end
                          >>up
                          >>with 'phantom' controls in tpMain's control collection.
                          >>>
                          >>Others might argue to the contrary, but I believe that the Dispose method
                          >>of
                          >>a CheckBox control does nothing special, and it certainly will not remove
                          >>itself from it's parent collection.
                          >>>
                          >>Therefore the code can be further simplified to:
                          >>>
                          >> For _i = tpMain.Controls .Count - 1 To 0 Step -1
                          >> Dim _ctrl = tpMain.Controls (_i)
                          >> If TypeOf (_ctrl) Is CheckBox AndAlso _ctrl.Name.Star tsWith("chkS")
                          >>Then
                          >>tpMain.Contro ls.RemoveAt(_i)
                          >> Next
                          >>>
                          >>To save you asking, the reverse loop is to ensure that iterating on the
                          >>control collection is still viable even when one or more of the iteration
                          >>actually removes an element.
                          >>>
                          >>>
                          >>>
                          >>"Siv" <Siv@discussion s.microsoft.com wrote in message
                          >>news:3A9061 98-C470-43E1-9A2D-3A3E2CDF834E@mi crosoft.com...
                          >Stephany,
                          >Thanks for your reply. Can you explain why the additional command
                          >>
                          >tpMain.Control s.RemoveAt(_i)
                          >>
                          >will do the trick where just doing the dispose on its own does not?
                          >>
                          >I appreciate you gave me the answer, but I try to learn from my
                          >questions
                          >here. Don't worry if you are too busy to reply, but if you do have time
                          >I
                          >would appreciate it.
                          >>
                          >Siv
                          >--
                          >Martley, Near Worcester, UK
                          >>
                          >>
                          >"Stephany Young" wrote:
                          >>
                          >>For _i = tpMain.Controls .Count - 1 To 0 Step -1
                          >> Dim _ctrl = tpMain.Controls (_i)
                          >> If TypeOf (_ctrl) Is CheckBox AndAlso _ctrl.Name.Star tsWith("chkS")
                          >>Then
                          >> tpMain.Controls .RemoveAt(_i)
                          >> _ctrl.Dispose()
                          >> End If
                          >>Next
                          >>>
                          >>>
                          >>"Siv" <Siv@discussion s.microsoft.com wrote in message
                          >>news:ECC510 2D-8DAC-4D39-B504-0F4E8BD8A6F8@mi crosoft.com...
                          >>I have a form that I programmaticall y generate some check boxes and
                          >>labels
                          >>on.
                          >Later on when I want to draw the form with different data I want to
                          >clear
                          >the previously created items and then put some new ones on.
                          >>
                          >In my code I am doing the following:
                          >>
                          >For Each ctrl In tpMain.Controls
                          >If TypeOf (ctrl) Is CheckBox Then
                          > If ctrl.Name.Start sWith("chkS") Then
                          > ctrl.Visible = False
                          > ctrl.Dispose()
                          > End If
                          > End If
                          >End if
                          >Next
                          >>
                          >What I am finding however is that in some cases controls that I know
                          >have
                          >been affected by the above loop are staying on the form and not
                          >being
                          >made
                          >invisible and then being disposed.
                          >>
                          >Is there a sure fire method of removing the controls rather than the
                          >dispose
                          >method which presumably is not always removing the control for
                          >whatever
                          >reason??
                          >>
                          >Siv
                          >--
                          >Martley, Near Worcester, UK
                          >>>
                          >>>
                          >>>
                          >>>

                          Comment

                          • Cor Ligthert [MVP]

                            #14
                            Re: How to clear controls from a form - Help Needed

                            Jack,

                            Absolute true, I had it first in my message but was afraid that it would
                            create to much confusion (and discussion). I normally try to write that part
                            exactly like you do, and it is very important in the case there are
                            unmanaged resources used. Which is AFAIK by instance not the fact "in"
                            controls. (but can be used in objects that controls are using).

                            I have seen it so much written in the context like I did now, so I did that
                            a bit like that to avoid a discussion. Normally I don't use that because it
                            is for me really a minor aspect to take action to clear 100bytes memory a
                            little bit quicker while I have 1Gb not used.

                            Cor

                            "Jack Jackson" <jjackson@cinno vations.netschr eef in bericht
                            news:6trf94l793 52peqi1qep0et83 j3ut6qdev@4ax.c om...
                            Cor,
                            >
                            I'm sure it is useless to argue with you, but it is misleading to say
                            that the purpose of Dispose is to tell the GC that the object " 'can
                            be' ready to clear up".
                            >
                            The purpose if Dispose is to release unmanaged resources. For
                            Controls, that would mostly be the Windows handle for the associated
                            Windows control. If you fail to call Dispose for a Control that has
                            allocated a Windows handle, then the handle might no be freed for a
                            very long time, and if you do this enough times you might run out of
                            Windows handles.
                            >
                            On Tue, 5 Aug 2008 06:47:35 +0200, "Cor Ligthert[MVP]"
                            <notmyfirstname @planet.nlwrote :
                            >
                            >>Siv,
                            >>
                            >>Disposing is not a removing method or whateverf, it is just telling the GC
                            >>that it "can be" ready to clear up, like the GC does that automaticly when
                            >>it is really needed. Dispose is meant to put it at the garbage. As
                            >>anology,
                            >>some people are the whole day busy with cleaning up there desktop (putting
                            >>it in the trash bin, which still will not be direct emptied), others do
                            >>that
                            >>once a day or/and some when that is needed.
                            >>
                            >>As it is about forms, you can see in your designer part of the class
                            >>(versions before 2005 normal in the class) the implementing of IDisposable
                            >>of a form.
                            >>
                            >>As long as an object is referenced by something (like on a form with
                            >>controls.ad d) it will not be cleared as long as that has a reference,
                            >>whatever other method you call (including dispose), mostly is that by
                            >>clear
                            >>them all in one time or by removing them one by one.
                            >>
                            >>Cor
                            >>
                            >>
                            >>"Siv" <Siv@discussion s.microsoft.com schreef in bericht
                            >>news:EBBE4C 00-2842-475E-97E5-01F9D7491B53@mi crosoft.com...
                            >>Stephany,
                            >>>
                            >>Thanks for clarifying, clearly I thought that to dispose of a control
                            >>would
                            >>by inference remove it from the tpMain collection of controls. It's the
                            >>old
                            >>story "never assume".
                            >>>
                            >>Thanks for your help.
                            >>>
                            >>Siv
                            >>--
                            >>Martley, Near Worcester, UK
                            >>>
                            >>>
                            >>"Stephany Young" wrote:
                            >>>
                            >>>Your problem was that, although you were 'disposing' the control, you
                            >>>were
                            >>>not removing it from tpMain's collection of controls. Therefore you
                            >>>end
                            >>>up
                            >>>with 'phantom' controls in tpMain's control collection.
                            >>>>
                            >>>Others might argue to the contrary, but I believe that the Dispose
                            >>>method
                            >>>of
                            >>>a CheckBox control does nothing special, and it certainly will not
                            >>>remove
                            >>>itself from it's parent collection.
                            >>>>
                            >>>Therefore the code can be further simplified to:
                            >>>>
                            >>> For _i = tpMain.Controls .Count - 1 To 0 Step -1
                            >>> Dim _ctrl = tpMain.Controls (_i)
                            >>> If TypeOf (_ctrl) Is CheckBox AndAlso _ctrl.Name.Star tsWith("chkS")
                            >>>Then
                            >>>tpMain.Contr ols.RemoveAt(_i )
                            >>> Next
                            >>>>
                            >>>To save you asking, the reverse loop is to ensure that iterating on the
                            >>>control collection is still viable even when one or more of the
                            >>>iteration
                            >>>actually removes an element.
                            >>>>
                            >>>>
                            >>>>
                            >>>"Siv" <Siv@discussion s.microsoft.com wrote in message
                            >>>news:3A90619 8-C470-43E1-9A2D-3A3E2CDF834E@mi crosoft.com...
                            >>Stephany,
                            >>Thanks for your reply. Can you explain why the additional command
                            >>>
                            >>tpMain.Contro ls.RemoveAt(_i)
                            >>>
                            >>will do the trick where just doing the dispose on its own does not?
                            >>>
                            >>I appreciate you gave me the answer, but I try to learn from my
                            >>questions
                            >>here. Don't worry if you are too busy to reply, but if you do have
                            >>time
                            >>I
                            >>would appreciate it.
                            >>>
                            >>Siv
                            >>--
                            >>Martley, Near Worcester, UK
                            >>>
                            >>>
                            >>"Stephany Young" wrote:
                            >>>
                            >>>For _i = tpMain.Controls .Count - 1 To 0 Step -1
                            >>> Dim _ctrl = tpMain.Controls (_i)
                            >>> If TypeOf (_ctrl) Is CheckBox AndAlso
                            >>>_ctrl.Name.S tartsWith("chkS ")
                            >>>Then
                            >>> tpMain.Controls .RemoveAt(_i)
                            >>> _ctrl.Dispose()
                            >>> End If
                            >>>Next
                            >>>>
                            >>>>
                            >>>"Siv" <Siv@discussion s.microsoft.com wrote in message
                            >>>news:ECC5102 D-8DAC-4D39-B504-0F4E8BD8A6F8@mi crosoft.com...
                            >>>I have a form that I programmaticall y generate some check boxes and
                            >>>labels
                            >>>on.
                            >>Later on when I want to draw the form with different data I want
                            >>to
                            >>clear
                            >>the previously created items and then put some new ones on.
                            >>>
                            >>In my code I am doing the following:
                            >>>
                            >>For Each ctrl In tpMain.Controls
                            >>If TypeOf (ctrl) Is CheckBox Then
                            >> If ctrl.Name.Start sWith("chkS") Then
                            >> ctrl.Visible = False
                            >> ctrl.Dispose()
                            >> End If
                            >> End If
                            >>End if
                            >>Next
                            >>>
                            >>What I am finding however is that in some cases controls that I
                            >>know
                            >>have
                            >>been affected by the above loop are staying on the form and not
                            >>being
                            >>made
                            >>invisible and then being disposed.
                            >>>
                            >>Is there a sure fire method of removing the controls rather than
                            >>the
                            >>dispose
                            >>method which presumably is not always removing the control for
                            >>whatever
                            >>reason??
                            >>>
                            >>Siv
                            >>--
                            >>Martley, Near Worcester, UK
                            >>>>
                            >>>>
                            >>>>
                            >>>>

                            Comment

                            • Cor Ligthert [MVP]

                              #15
                              Re: How to clear controls from a form - Help Needed

                              Jack,

                              Maybe should I have added this piece of code that is created by most
                              designers.
                              Which AFAIK is done by instance at every close of a form.

                              \\\
                              'Form overrides dispose to clean up the component list.
                              <System.Diagnos tics.DebuggerNo nUserCode()_
                              Protected Overrides Sub Dispose(ByVal disposing As Boolean)
                              Try
                              If disposing AndAlso components IsNot Nothing Then
                              components.Disp ose()
                              End If
                              Finally
                              MyBase.Dispose( disposing)
                              End Try
                              End Sub
                              ///

                              Cor

                              "Jack Jackson" <jjackson@cinno vations.netschr eef in bericht
                              news:6trf94l793 52peqi1qep0et83 j3ut6qdev@4ax.c om...
                              Cor,
                              >
                              I'm sure it is useless to argue with you, but it is misleading to say
                              that the purpose of Dispose is to tell the GC that the object " 'can
                              be' ready to clear up".
                              >
                              The purpose if Dispose is to release unmanaged resources. For
                              Controls, that would mostly be the Windows handle for the associated
                              Windows control. If you fail to call Dispose for a Control that has
                              allocated a Windows handle, then the handle might no be freed for a
                              very long time, and if you do this enough times you might run out of
                              Windows handles.
                              >
                              On Tue, 5 Aug 2008 06:47:35 +0200, "Cor Ligthert[MVP]"
                              <notmyfirstname @planet.nlwrote :
                              >
                              >>Siv,
                              >>
                              >>Disposing is not a removing method or whateverf, it is just telling the GC
                              >>that it "can be" ready to clear up, like the GC does that automaticly when
                              >>it is really needed. Dispose is meant to put it at the garbage. As
                              >>anology,
                              >>some people are the whole day busy with cleaning up there desktop (putting
                              >>it in the trash bin, which still will not be direct emptied), others do
                              >>that
                              >>once a day or/and some when that is needed.
                              >>
                              >>As it is about forms, you can see in your designer part of the class
                              >>(versions before 2005 normal in the class) the implementing of IDisposable
                              >>of a form.
                              >>
                              >>As long as an object is referenced by something (like on a form with
                              >>controls.ad d) it will not be cleared as long as that has a reference,
                              >>whatever other method you call (including dispose), mostly is that by
                              >>clear
                              >>them all in one time or by removing them one by one.
                              >>
                              >>Cor
                              >>
                              >>
                              >>"Siv" <Siv@discussion s.microsoft.com schreef in bericht
                              >>news:EBBE4C 00-2842-475E-97E5-01F9D7491B53@mi crosoft.com...
                              >>Stephany,
                              >>>
                              >>Thanks for clarifying, clearly I thought that to dispose of a control
                              >>would
                              >>by inference remove it from the tpMain collection of controls. It's the
                              >>old
                              >>story "never assume".
                              >>>
                              >>Thanks for your help.
                              >>>
                              >>Siv
                              >>--
                              >>Martley, Near Worcester, UK
                              >>>
                              >>>
                              >>"Stephany Young" wrote:
                              >>>
                              >>>Your problem was that, although you were 'disposing' the control, you
                              >>>were
                              >>>not removing it from tpMain's collection of controls. Therefore you
                              >>>end
                              >>>up
                              >>>with 'phantom' controls in tpMain's control collection.
                              >>>>
                              >>>Others might argue to the contrary, but I believe that the Dispose
                              >>>method
                              >>>of
                              >>>a CheckBox control does nothing special, and it certainly will not
                              >>>remove
                              >>>itself from it's parent collection.
                              >>>>
                              >>>Therefore the code can be further simplified to:
                              >>>>
                              >>> For _i = tpMain.Controls .Count - 1 To 0 Step -1
                              >>> Dim _ctrl = tpMain.Controls (_i)
                              >>> If TypeOf (_ctrl) Is CheckBox AndAlso _ctrl.Name.Star tsWith("chkS")
                              >>>Then
                              >>>tpMain.Contr ols.RemoveAt(_i )
                              >>> Next
                              >>>>
                              >>>To save you asking, the reverse loop is to ensure that iterating on the
                              >>>control collection is still viable even when one or more of the
                              >>>iteration
                              >>>actually removes an element.
                              >>>>
                              >>>>
                              >>>>
                              >>>"Siv" <Siv@discussion s.microsoft.com wrote in message
                              >>>news:3A90619 8-C470-43E1-9A2D-3A3E2CDF834E@mi crosoft.com...
                              >>Stephany,
                              >>Thanks for your reply. Can you explain why the additional command
                              >>>
                              >>tpMain.Contro ls.RemoveAt(_i)
                              >>>
                              >>will do the trick where just doing the dispose on its own does not?
                              >>>
                              >>I appreciate you gave me the answer, but I try to learn from my
                              >>questions
                              >>here. Don't worry if you are too busy to reply, but if you do have
                              >>time
                              >>I
                              >>would appreciate it.
                              >>>
                              >>Siv
                              >>--
                              >>Martley, Near Worcester, UK
                              >>>
                              >>>
                              >>"Stephany Young" wrote:
                              >>>
                              >>>For _i = tpMain.Controls .Count - 1 To 0 Step -1
                              >>> Dim _ctrl = tpMain.Controls (_i)
                              >>> If TypeOf (_ctrl) Is CheckBox AndAlso
                              >>>_ctrl.Name.S tartsWith("chkS ")
                              >>>Then
                              >>> tpMain.Controls .RemoveAt(_i)
                              >>> _ctrl.Dispose()
                              >>> End If
                              >>>Next
                              >>>>
                              >>>>
                              >>>"Siv" <Siv@discussion s.microsoft.com wrote in message
                              >>>news:ECC5102 D-8DAC-4D39-B504-0F4E8BD8A6F8@mi crosoft.com...
                              >>>I have a form that I programmaticall y generate some check boxes and
                              >>>labels
                              >>>on.
                              >>Later on when I want to draw the form with different data I want
                              >>to
                              >>clear
                              >>the previously created items and then put some new ones on.
                              >>>
                              >>In my code I am doing the following:
                              >>>
                              >>For Each ctrl In tpMain.Controls
                              >>If TypeOf (ctrl) Is CheckBox Then
                              >> If ctrl.Name.Start sWith("chkS") Then
                              >> ctrl.Visible = False
                              >> ctrl.Dispose()
                              >> End If
                              >> End If
                              >>End if
                              >>Next
                              >>>
                              >>What I am finding however is that in some cases controls that I
                              >>know
                              >>have
                              >>been affected by the above loop are staying on the form and not
                              >>being
                              >>made
                              >>invisible and then being disposed.
                              >>>
                              >>Is there a sure fire method of removing the controls rather than
                              >>the
                              >>dispose
                              >>method which presumably is not always removing the control for
                              >>whatever
                              >>reason??
                              >>>
                              >>Siv
                              >>--
                              >>Martley, Near Worcester, UK
                              >>>>
                              >>>>
                              >>>>
                              >>>>

                              Comment

                              Working...