title: Custom Drawing of (multi-layered)Embedded TabControls Error
body: <p>I have a winform app that has tabcontrols that are 3 layers deep.  I am dynamically coloring the tabs with the below class.  When it goes to color an embedded tabcontrol it pitches a fit.  </p>
  
  <pre><code>A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.Windows.Forms.dll
  </code></pre>
  
  <p>Do I need to do something different for those?  If I comment out the embedded forms calls to tabRenderer then i do not get these errors. Am I not disposing of my TabRenderer object properly?</p>
  
  <p>Is it maybe something else entirely?  The way I am embeding the tab controls?</p>
  
  <p>An example of what my program currently looks like is here --></p>
  
  <a href="http://picasaweb.google.com/lh/photo/MoJ9OhnRGsJSvIydBPjWiw?feat=embedwebsite" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Uxerf.jpg" /></a><br/><sub>(source: <a href="http://lh6.ggpht.com/_DqVwi_JXyS0/Sf8EI0ETYrI/AAAAAAAABhI/pmAhozErubo/s288/MATRIXUIExample.JPG" rel="nofollow noreferrer">ggpht.com</a>)</sub><br/>From <a href="http://picasaweb.google.com/TheSchallers/DevFiles?feat=embedwebsite" rel="nofollow noreferrer">DevFiles</a>
  
  <p>As you can see there are 3 layers of tab controls.  This occurs twice in the program and both cause the mentioned error.  There are 6 calls to tabRenderer in total as there are 5 tab controls. 1 Top Level, 3 Second Level and 2 Third Level.</p>
  
  <p>The code being used to Color the Tab Controls:</p>
  
  <pre><code>public class psTabRenderer
  {
      private TabControl _tabControl;
      private Color _fillColor;
      private Color _selectedFillColor;
      private Color _textColor;
      private Color _selectedTextColor;
  
      public psTabRenderer(TabControl tabControl, Color fillColor, Color selectedFillColor, Color textColor, Color selectedTextColor)
      {
          _tabControl = tabControl;
          _fillColor = fillColor;
          _selectedFillColor = selectedFillColor;
          _textColor = textColor;
          _selectedTextColor = selectedTextColor;
  
          _tabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
          _tabControl.DrawItem += TabControlDrawItem;
      }
  
      private void TabControlDrawItem(object sender, DrawItemEventArgs e)
      {
          TabPage currentTab = _tabControl.TabPages[e.Index];
          Rectangle itemRect = _tabControl.GetTabRect(e.Index);
          var fillBrush = new SolidBrush(_fillColor);
          var textBrush = new SolidBrush(_textColor);
          var sf = new StringFormat
          {
              Alignment = StringAlignment.Center,
              LineAlignment = StringAlignment.Center
          };
  
          //If we are currently painting the Selected TabItem we'll
          //change the brush colors and inflate the rectangle.
          if (Convert.ToBoolean(e.State &amp; DrawItemState.Selected))
          {
              fillBrush.Color = _selectedFillColor;
              textBrush.Color = _selectedTextColor;
              itemRect.Inflate(2, 2);
          }
  
          //Set up rotation for left and right aligned tabs
          if (_tabControl.Alignment == TabAlignment.Left || _tabControl.Alignment == TabAlignment.Right)
          {
              float rotateAngle = 90;
              if (_tabControl.Alignment == TabAlignment.Left)
                  rotateAngle = 270;
              var cp = new PointF(itemRect.Left + (itemRect.Width / 2), itemRect.Top + (itemRect.Height / 2));
              e.Graphics.TranslateTransform(cp.X, cp.Y);
              e.Graphics.RotateTransform(rotateAngle);
              itemRect = new Rectangle(-(itemRect.Height / 2), -(itemRect.Width / 2), itemRect.Height, itemRect.Width);
          }
  
          //Next we'll paint the TabItem with our Fill Brush
          e.Graphics.FillRectangle(fillBrush, itemRect);
  
          //Now draw the text.
          e.Graphics.DrawString(currentTab.Text, e.Font, textBrush, (RectangleF)itemRect, sf);
  
          //Reset any Graphics rotation
          e.Graphics.ResetTransform();
  
          //Finally, we should Dispose of our brushes.
          fillBrush.Dispose();
          textBrush.Dispose();
      }
  }
  </code></pre>
  
  <p>And this is how I call it:</p>
  
  <pre><code>        private void frmMCPEmployment_Load(object sender, EventArgs e)
      {
          FormPaint();
      }
  
      public void FormPaint()
      {
          // ToDo: This call to the Tab Renderer is throwing a Win32 "Error Creating Window Handle" 
          new psTabRenderer(tclEmployment, Color.LightSteelBlue, Color.Khaki, Color.Black, Color.Black);
      }
  </code></pre>