title: Deleting all components of a certain class on a form (Delphi)
body: <p>This is probably a stupid question, but my brain is just cooked enough I think I'm going to use one of my "lifelines" to see if I can get some help from my stack overflow friends. ;)</p>
  
  <p>I need to delete all occurrences of a particular component type on my main form (some of them are inside panels or tabsheets, but all on and owned by the same form).  Here's what I have now:</p>
  
  <pre><code>for i := 0 to frmMain.ComponentCount - 1 do  
    begin  
      if frmMain.Components[i] is TMyClass then frmMain.Components[i].Destroy;  
    end;    
  </code></pre>
  
  <p>The problem is (and I knew it would be before I compiled it) that once I destroy the component, the form's component list re-indexes and I end up out of bounds.</p>
  
  <p>What's the best way to solve this?  I thought about adding the "found" components to a standalone array, and then walk through that after this loop to delete them, which I think will work.... but is that the best approach?</p>
  
  <p>TIA</p>
  
  <hr>
  
  <p>UPDATE: </p>
  
  <p>You guys rock.  Thanks. : )</p>