title: Correctly disposing user controls and child controls in C#
body: <p>we are experiencing a few problems with the IDisposable pattern. In this case there is a user control 'ControlA' with a FlowLayoutPanel, which contains more usercontrols 'ControlB'.</p>
  
  <p>When calling Dispose(bool), I check if disposing if true, and if IsDisposed is false. I then try to explicitly dispose each ControlB in the FlowLayoutPanel's Controls collection.
  However, if does not loop through all controls, only 3 of 8, or 2 of 4.</p>
  
  <pre><code>Dispose(bool disposing)
  {
    if (disposing)
    {
      if (!IsDisposed)
      {
        //unhook events etc.
  
        foreach(ControlB ctrl in flowlayoutpanel.Controls)  //&lt;-- there 8 controls
          ctrl.Dispose(); //&lt;-- called 3 times only
  
        flp.Controls.Clear();
      }
    }
    //make all members null
  }
  </code></pre>
  
  <p>My quesitons are: 1. Why is this happening?
  2. What are best practices and expereinces you guys had with disposing user controls and thei child controls? E.g. do you unsubscribe event handlers at all times, etc.</p>
  
  <p>Thanks!</p>