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.

A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.Windows.Forms.dll

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?

Is it maybe something else entirely? The way I am embeding the tab controls?

An example of what my program currently looks like is here -->

From DevFiles

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.

The code being used to Color the Tab Controls:

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 & 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);
            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

        //Finally, we should Dispose of our brushes.

And this is how I call it:

        private void frmMCPEmployment_Load(object sender, EventArgs e)

    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);
Was it helpful?


Okay, I answered my own question.(kinda)

I believe what was happening was that as my app loads it starts firing off each Forms Load() event which in turn fires of it's embedded Forms Load() event and so on. I had thrown my call to TabRenderer in the load Event and something that I don't understand was happening. I pulled that call out to a PaintTabs() function and then wait for the first to finish before it calls the next(I think?).

Either way it no longer generates any errors. I now call it like so from the TOP LEVEL TabControl:

        public void PaintTabs()
        new psTabRenderer(tclWWCModuleHost, Color.LightSteelBlue, Color.Khaki, Color.Black, Color.Black);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow