Example #1
0
        /// <summary>
        ///  When the verb is invoked, change the parent of the ToolStrip.
        /// </summary>
        // This is actually called...
        public void ChangeParent()
        {
            Cursor current = Cursor.Current;
            // create a transaction so this happens as an atomic unit.
            DesignerTransaction changeParent = _host.CreateTransaction("Add ToolStripContainer Transaction");

            try
            {
                Cursor.Current = Cursors.WaitCursor;
                //Add a New ToolStripContainer to the RootComponent ...
                Control root = _host.RootComponent as Control;
                if (_host.GetDesigner(root) is ParentControlDesigner rootDesigner)
                {
                    // close the DAP first - this is so that the autoshown panel on drag drop here is not conflicting with the currently opened panel
                    // if the verb was called from the panel
                    ToolStrip toolStrip = _designer.Component as ToolStrip;
                    if (toolStrip != null && _designer != null && _designer.Component != null && _provider != null)
                    {
                        DesignerActionUIService dapuisvc = _provider.GetService(typeof(DesignerActionUIService)) as DesignerActionUIService;
                        dapuisvc.HideUI(toolStrip);
                    }

                    // Get OleDragHandler ...
                    ToolboxItem        tbi = new ToolboxItem(typeof(ToolStripContainer));
                    OleDragDropHandler ddh = rootDesigner.GetOleDragHandler();
                    if (ddh != null)
                    {
                        IComponent[] newComp = ddh.CreateTool(tbi, root, 0, 0, 0, 0, false, false);
                        if (newComp[0] is ToolStripContainer tsc)
                        {
                            if (toolStrip != null)
                            {
                                var                changeService = _provider.GetService <IComponentChangeService>();
                                Control            newParent     = GetParent(tsc, toolStrip);
                                PropertyDescriptor controlsProp  = TypeDescriptor.GetProperties(newParent)["Controls"];
                                Control            oldParent     = toolStrip.Parent;
                                if (oldParent != null)
                                {
                                    changeService.OnComponentChanging(oldParent, controlsProp);
                                    //remove control from the old parent
                                    oldParent.Controls.Remove(toolStrip);
                                }

                                if (newParent != null)
                                {
                                    changeService.OnComponentChanging(newParent, controlsProp);
                                    //finally add & relocate the control with the new parent
                                    newParent.Controls.Add(toolStrip);
                                }

                                //fire our comp changed events
                                if (changeService != null && oldParent != null && newParent != null)
                                {
                                    changeService.OnComponentChanged(oldParent, controlsProp);
                                    changeService.OnComponentChanged(newParent, controlsProp);
                                }

                                //Set the Selection on the new Parent ... so that the selection is restored to the new item,
                                if (_provider.GetService(typeof(ISelectionService)) is ISelectionService selSvc)
                                {
                                    selSvc.SetSelectedComponents(new IComponent[] { tsc });
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (e is InvalidOperationException)
                {
                    IUIService uiService = (IUIService)_provider.GetService(typeof(IUIService));
                    uiService.ShowError(e.Message);
                }

                if (changeParent != null)
                {
                    changeParent.Cancel();
                    changeParent = null;
                }
            }
            finally
            {
                if (changeParent != null)
                {
                    changeParent.Commit();
                }

                Cursor.Current = current;
            }
        }
        public void ChangeParent()
        {
            Cursor current = Cursor.Current;
            DesignerTransaction transaction = this._host.CreateTransaction("Add ToolStripContainer Transaction");

            try
            {
                Cursor.Current = Cursors.WaitCursor;
                Control rootComponent          = this._host.RootComponent as Control;
                ParentControlDesigner designer = this._host.GetDesigner(rootComponent) as ParentControlDesigner;
                if (designer != null)
                {
                    ToolStrip component = this._designer.Component as ToolStrip;
                    if (((component != null) && (this._designer != null)) && ((this._designer.Component != null) && (this._provider != null)))
                    {
                        (this._provider.GetService(typeof(DesignerActionUIService)) as DesignerActionUIService).HideUI(component);
                    }
                    ToolboxItem        tool           = new ToolboxItem(typeof(ToolStripContainer));
                    OleDragDropHandler oleDragHandler = designer.GetOleDragHandler();
                    if (oleDragHandler != null)
                    {
                        ToolStripContainer container = oleDragHandler.CreateTool(tool, rootComponent, 0, 0, 0, 0, false, false)[0] as ToolStripContainer;
                        if ((container != null) && (component != null))
                        {
                            IComponentChangeService service = this._provider.GetService(typeof(IComponentChangeService)) as IComponentChangeService;
                            Control            parent       = this.GetParent(container, component);
                            PropertyDescriptor member       = TypeDescriptor.GetProperties(parent)["Controls"];
                            Control            control3     = component.Parent;
                            if (control3 != null)
                            {
                                service.OnComponentChanging(control3, member);
                                control3.Controls.Remove(component);
                            }
                            if (parent != null)
                            {
                                service.OnComponentChanging(parent, member);
                                parent.Controls.Add(component);
                            }
                            if (((service != null) && (control3 != null)) && (parent != null))
                            {
                                service.OnComponentChanged(control3, member, null, null);
                                service.OnComponentChanged(parent, member, null, null);
                            }
                            ISelectionService service3 = this._provider.GetService(typeof(ISelectionService)) as ISelectionService;
                            if (service3 != null)
                            {
                                service3.SetSelectedComponents(new IComponent[] { container });
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (exception is InvalidOperationException)
                {
                    ((IUIService)this._provider.GetService(typeof(IUIService))).ShowError(exception.Message);
                }
                if (transaction != null)
                {
                    transaction.Cancel();
                    transaction = null;
                }
            }
            finally
            {
                if (transaction != null)
                {
                    transaction.Commit();
                    transaction = null;
                }
                Cursor.Current = current;
            }
        }