/// <summary>
        /// Selects the tab at the specified index.
        /// </summary>
        /// <param name="i">The index of the tab to be selected.</param>
        private void SelectTab(int i)
        {
            if (mSelectedTab != i && i >= 0 && i < mTabPages.Count)
            {
                if (mSelectedTab >= 0 && mSelectedTab < mTabPages.Count)
                {
                    mTabPages[mSelectedTab].DeselectTab();
                    //this.Controls.Remove(mTabPages[mSelectedTab]);
                    mTabPages[mSelectedTab].Visible = false;
                }

                if (TabInView(i))
                {
                    mSelectedTab = i;
                    //this.Controls.Add(mTabPages[i]);
                    mTabPages[i].Visible = true;
                    mTabPages[i].SelectTab();
                }
                else
                {
                    DynamicTabPage tab = mTabPages[i];
                    mTabPages.RemoveAt(i);
                    mTabPages.Insert(0, tab);
                    mSelectedTab = 0;
                    //this.Controls.Add(mTabPages[0]);
                    mTabPages[0].Visible = true;
                    mTabPages[0].SelectTab();
                }

                OnSelectedIndexChanged(this, new EventArgs());

                Invalidate();
            }
        }
        /// <summary>
        /// Called when a tab page has been removed.
        /// </summary>
        /// <param name="index">The index of the removed tab page.</param>
        /// <param name="page">The tab page being removed.</param>
        internal void OnTabPageRemoved(int index, DynamicTabPage page)
        {
            page.TextChanged -= new EventHandler(this.DynamicTabControl_TextChanged);

            if (index == mSelectedTab)
            {
                page.DeselectTab();
                this.Controls.Remove(page);

                if (mSelectedTab < mTabPages.Count)
                {
                    //this.Controls.Add(mTabPages[mSelectedTab]);
                    mTabPages[mSelectedTab].Visible = true;
                    mTabPages[mSelectedTab].SelectTab();
                }
                else
                {
                    mSelectedTab = mTabPages.Count - 1;

                    if (mSelectedTab >= 0)
                    {
                        //this.Controls.Add(mTabPages[mSelectedTab]);
                        mTabPages[mSelectedTab].Visible = true;
                        mTabPages[mSelectedTab].SelectTab();
                    }
                }

                OnSelectedIndexChanged(this, new EventArgs());
            }
            else if (index < mSelectedTab)
            {
                --mSelectedTab;
                OnSelectedIndexChanged(this, new EventArgs());
            }

            Invalidate();
        }
        /// <summary>
        /// Called to draw a tab.
        /// </summary>
        /// <param name="offX">The X offset of the tab.</param>
        /// <param name="size">The size of the tab in pixels.</param>
        /// <param name="graphics">The GDI+ graphics object.</param>
        /// <param name="text">The text to be displayed on the tab.</param>
        /// <param name="fmt">The format of the text.</param>
        /// <param name="clip">The clipping region.</param>
        /// <param name="selectedTab">The currently selected tab.</param>
        private void DrawTab(int offX, SizeF size, Graphics graphics, string text, StringFormat fmt, bool clip, bool selectedTab, DynamicTabPage tabPage)
        {
            Rectangle tabRect = new Rectangle(offX, 3, (int)size.Width + SLIDEWIDTH, TABHEIGHT);

            Point[] points =
            {
                new Point(tabRect.Left,                tabRect.Bottom),
                new Point(tabRect.Left + CURVEOFFSETX, tabRect.Bottom - CURVEOFFSETY),
                new Point(tabRect.Left + SLIDEWIDTH,   tabRect.Top),
                new Point(tabRect.Right - 2,           tabRect.Top),
                new Point(tabRect.Right,               tabRect.Top + 2),
                new Point(tabRect.Right,               tabRect.Bottom),
            };

            if (clip)
            {
                graphics.Clip = new Region(new Rectangle(tabRect.Left + SLIDEWIDTH / 2 + 1, tabRect.Top, tabRect.Width, tabRect.Height + 1));
            }
            else
            {
                graphics.Clip = new Region();
            }

            graphics.FillPolygon(tabPage.TabBackgroundColor, points);
            graphics.DrawPolygon(SystemPens.ControlDark, points);

            RectangleF rect = new RectangleF((float)(tabRect.Left + CURVEOFFSETX), (float)tabRect.Top, (float)(tabRect.Right - tabRect.Left - CURVEOFFSETX), (float)tabRect.Bottom - (float)tabRect.Top);

            graphics.DrawString(text, selectedTab ? mBoldFont : this.Font, tabPage.TabForegroundColor, rect, fmt);

            if (selectedTab)
            {
                graphics.DrawLine(SystemPens.Window, tabRect.Left + 1, tabRect.Bottom, tabRect.Right - 1, tabRect.Bottom);
            }
        }