private void OnComponentRemoving(object sender, ComponentEventArgs e)
        {
            // If our workspace is being removed
            if (e.Component == _workspace)
            {
                // Prevent layout being performed during removal of children otherwise the layout
                // code will cause the controls to be added back before they are actually destroyed
                _workspace.SuspendLayout();

                // Need access to host in order to delete a component
                IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));

                // We need to remove all children from the workspace
                for (int i = _workspace.Root.Children.Count - 1; i >= 0; i--)
                {
                    Component comp = _workspace.Root.Children[i] as Component;

                    // If the component is a control...
                    if (comp is Control)
                    {
                        // We need to manually remove it from the workspace controls collection
                        KryptonReadOnlyControls readOnlyControls = (KryptonReadOnlyControls)_workspace.Controls;
                        readOnlyControls.RemoveInternal(comp as Control);
                    }

                    host.DestroyComponent(comp);

                    // Must remove the child after it has been destroyed otherwise the component destroy method
                    // will not be able to climb the sequence chain to find the parent workspace instance
                    _workspace.Root.Children.Remove(comp);
                }

                _workspace.ResumeLayout();
            }
        }
        private void OnComponentRemoving(object sender, ComponentEventArgs e)
        {
            // If our sequence is being removed
            if (e.Component == _sequence)
            {
                // Need access to host in order to delete a component
                IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));

                // Climb the workspace item tree to get the top most sequence
                KryptonWorkspace workspace     = null;
                IWorkspaceItem   workspaceItem = _sequence;
                while (workspaceItem.WorkspaceParent != null)
                {
                    workspaceItem = workspaceItem.WorkspaceParent;
                }

                // Grab the workspace control that contains the top most sequence
                if ((workspaceItem != null) && (workspaceItem is KryptonWorkspaceSequence))
                {
                    KryptonWorkspaceSequence sequence = (KryptonWorkspaceSequence)workspaceItem;
                    workspace = sequence.WorkspaceControl;
                }

                // We need to remove all children from the sequence
                for (int j = _sequence.Children.Count - 1; j >= 0; j--)
                {
                    Component comp = _sequence.Children[j] as Component;

                    // If the component is a control...
                    if ((comp is Control) && (workspace != null))
                    {
                        // We need to manually remove it from the workspace controls collection
                        KryptonReadOnlyControls readOnlyControls = (KryptonReadOnlyControls)workspace.Controls;
                        readOnlyControls.RemoveInternal(comp as Control);
                    }

                    host.DestroyComponent(comp);

                    // Must remove the child after it has been destroyed otherwise the component destroy method
                    // will not be able to climb the sequence chain to find the parent workspace instance
                    _sequence.Children.Remove(comp);
                }
            }
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            try
            {
                // Must unhook to prevent memory leak
                _events         = false;
                Pages.Cleared  -= OnPagesChanged;
                Pages.Removed  -= OnPagesChanged;
                Pages.Inserted -= OnPagesChanged;

                // Must remove from parent workspace manually because the control collection is readonly
                if (Parent != null)
                {
                    KryptonReadOnlyControls controls = (KryptonReadOnlyControls)Parent.Controls;
                    controls.RemoveInternal(this);
                }

                base.Dispose(disposing);
            }
            catch { }
        }