VB .NET Strange Behaviour When Removing Form Controls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sgeklor
    New Member
    • Oct 2007
    • 5

    VB .NET Strange Behaviour When Removing Form Controls

    Hi guys,

    I have a panel on a form and at runtime I create some controls on the panel. Then, also during runtime I want to clear the panel of all of its controls. The basic way to do this is with the following code:

    Code:
    For Each this_ctrl As Object In target_panel.Controls
         this_ctrl.Dispose()
    Next
    What happens when I use this code is that only every second control is removed from the panel! Yet, when I replace the .Dispose() method with some other behaviour (like change the control's colour) it appears that every control is accessed correctly. Repeating the code block iteratively removes every second control until none are left, but this is hardly an elegant way to proceed.

    So I think this is strange. Can anyone suggest a cause for the problem, or another way of clearing the panel?

    Thanks heaps,

    Ash

    PS: I'm using Visual Studio 2005
  • jigsmshah
    New Member
    • Aug 2007
    • 14

    #2
    Originally posted by sgeklor
    Hi guys,

    I have a panel on a form and at runtime I create some controls on the panel. Then, also during runtime I want to clear the panel of all of its controls. The basic way to do this is with the following code:

    Code:
    For Each this_ctrl As Object In target_panel.Controls
         this_ctrl.Dispose()
    Next
    What happens when I use this code is that only every second control is removed from the panel! Yet, when I replace the .Dispose() method with some other behaviour (like change the control's colour) it appears that every control is accessed correctly. Repeating the code block iteratively removes every second control until none are left, but this is hardly an elegant way to proceed.

    So I think this is strange. Can anyone suggest a cause for the problem, or another way of clearing the panel?

    Thanks heaps,

    Ash

    PS: I'm using Visual Studio 2005

    See it is not strange,you iterating the Collection object and remove the controls
    from it so the for loop wont iterate for the number of controls you have in the panel.Instead of Dispose you try to make controls invisible,the for loop will work properly as you are not deleting the controls from the ControlCollecti on.
    Hope it makes sense.

    try to use
    panel.Controls. Clear();

    Regards
    Jignesh

    Comment

    • sgeklor
      New Member
      • Oct 2007
      • 5

      #3
      Brilliant!!! The .Clear() method works perfectly.

      In general though, I didn't realise the limitations of "For Eaching" through an ArrayList.

      What if I had a general ArrayList and wanted to remove some elements from it (those that match some criteria) by "For Eaching" through the list. Then by disposing those members that I do not want will I not have the same problem?

      What is the correct way to iterate through and remove some members from an ArrayList?

      Thanks,

      Ash

      Comment

      Working...