/// <summary>
        /// Overrides the base class function.
        /// Before removing the control, the internal lists are updated depending on the type.
        /// </summary>
        /// <param name="e">A ControlEventArgs that contains the event data.</param>
        protected override void OnControlRemoved(ControlEventArgs e)
        {
            if (disableOnControlRemove)
            {
                if (e.Control is DockContainer)
                    containerList.Remove(e.Control);
                else if (e.Control is DockPanel)
                    panelList.Remove(e.Control);

                return;
            }

            if ((e.Control is DockPanel) && (e.Control != dragPanel))
            {
                // Remove panel from list object.
                panelList.Remove(e.Control);

                // Show or hide buttons.
                if (panelList.Count == 0)
                    HideButtons();
            }
            else if ((e.Control is DockContainer) && (containerList.Count == 2))
            {
                // Check, if the container is a drag dummy.
                if ((e.Control as DockContainer).isDragContainer)
                    return;

                // Remove container from list object.
                containerList.Remove(e.Control);

                // Remove the last container and copy all panels.
                DockContainer cont = containerList[0] as DockContainer;
                cont.disableOnControlRemove = true;

                containerList.Remove(cont);
                this.Controls.Remove(cont);
                this.DockType = cont.DockType;
                removeable = cont.removeable;

                while (cont.containerList.Count > 0)
                {
                    containerList.Add(cont.containerList[0] as DockContainer);
                    this.Controls.Add(cont.containerList[0] as DockContainer);
                }

                DockPanel temp = cont.ActivePanel;
                while (cont.panelList.Count > 0)
                    this.Controls.Add(cont.panelList[0] as DockPanel);
                ActivePanel = temp;

                cont.Dispose();
                cont = null;
            }

            // Adjust the borders of the container.
            AdjustBorders();

            // Retrieve the active panel.
            activePanel = null;
            if (panelList.Count > 0)
                ActivePanel = (DockPanel)panelList[panelList.Count-1];

            // Raise event and redraw client area.
            base.OnControlRemoved(e);
            Invalidate();
        }
        /// <summary>
        /// Overrides the base class function to handle drag of panels and resizing.
        /// </summary>
        /// <param name="e">A MouseEventArgs that contains the mouse data.</param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            // Destroy drag window.
            if (dragWindow != null)
            {
                dragWindow.Close();
                dragWindow.Dispose();
                dragWindow = null;
            }

            // Test header.
            Focus();

            ptStart = new Point(e.X, e.Y);

            // Test footer.
            if (panelList.Count > 1)
            {
                foreach (DockPanel panel in panelList)
                {
                    if (panel.TabRect.Contains(e.X, e.Y))
                    {
                        ActivePanel = panel;
                        return;
                    }
                }
            }

            base.OnMouseDown(e);
        }
        /// <summary>
        /// Selects a specific tab based on its reference pointer.
        /// </summary>
        /// <param name="p">The tab reference.</param>
        public void SelectTab(DockPanel p)
        {
            try
            {
                if ((panelList != null) && (p != null))
                {
                    if (!panelList.Contains(p))
                        return;

                    ActivePanel = p;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
 /// <summary>
 /// Selects a specific tab beginning with 0 from left to the right.
 /// </summary>
 /// <param name="i">The index of the tab.</param>
 public void SelectTab(int i)
 {
     try
     {
         if (panelList[i] != null)
             ActivePanel = panelList[i] as DockPanel;
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
        /// <summary>
        /// Adds a panel to the container.
        /// </summary>
        /// <param name="src">The source window of the panel.</param>
        /// <param name="pt">The target position.</param>
        public void AddPanel(DockWindow src, Point pt)
        {
            try
            {
                if (!src.IsLoaded)
                    src.CreateContainer();

                blockFocusEvents = true;
                DockPanel panel = src.ControlContainer;
                this.Controls.Add(panel);
                ActivePanel = panel;
                blockFocusEvents = false;

                if (!src.IsLoaded)
                    src.Show();

                src.Hide();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        /// <summary>
        /// Measures the size of a DockPanel tab depending on a given font.
        /// </summary>
        /// <param name="panel">The DockPanel object.</param>
        /// <param name="graphics">The Graphics interface.</param>
        /// <param name="cut">Enables the reduction of the panel size according to the available space.</param>
        /// <param name="font">The target font.</param>
        /// <returns>The size of the panel tab.</returns>
        private SizeF MeasurePanel(DockPanel panel, Graphics graphics, bool cut, Font font)
        {
            SizeF ret = graphics.MeasureString(panel.Form.Text, font);

            if (font.Bold)
                ret.Width += 12;
            else
                ret.Width += 6;

            if (showIcons && (panel.Form.Icon != null))
                ret.Width += ret.Height+3;

            if ((ret.Width > (Width-DockPadding.Left-DockPadding.Right+2-8)/panelList.Count) && cut)
                ret.Width = (Width-DockPadding.Left-DockPadding.Right+2-8)/panelList.Count;

            return ret;
        }
        /// <summary>
        /// A message handler for the Deactivate event of the parent window.
        /// Needed to refresh the child controls.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">An EventArgs that contains the event data.</param>
        private void DeactivateParent(object sender, EventArgs e)
        {
            try
            {
                foreach (DockPanel p in listDocument)
                {
                    DockContainer c = p.Form.HostContainer;
                    if (c == null)
                        continue;

                    if ((c.ActivePanel == p) && (c.ContainsFocus))
                    {
                        activeDoc = p;
                        p.SetFocus(false);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("DockManager.DeactivateParent: "+ex.Message);
            }
            finally
            {
                Invalidate(true);
            }
        }