private void AddSurfaceToNewTabPage(DesignSurfaceUView surface, string pageName) { var view = surface.GetView(); TabPage newPage = new TabPage(pageName); newPage.Name = pageName; newPage.SuspendLayout(); view.Dock = DockStyle.Fill; view.Parent = newPage; //- Note this assignment this.tbCtrlpDesigner.TabPages.Add(newPage); newPage.ResumeLayout(); //- select the TabPage created this.tbCtrlpDesigner.SelectedIndex = this.tbCtrlpDesigner.TabPages.Count - 1; }
public void RemoveDesignSurface(DesignSurfaceUView surfaceToErase) { try { //- remove the TabPage which has the same name of //- the RootComponent host by DesignSurface "surfaceToErase" //- Note: //- DesignSurfaceManager continues to reference the DesignSurface erased //- that Designsurface continue to exist but it is no more reachable //- this fact is usefull when generate new names for Designsurfaces just created //- avoiding name clashing var designHost = surfaceToErase.GetService(typeof(IDesignerHost)) as IDesignerHost; string dsRootComponentName = designHost?.RootComponent.Site.Name; TabPage tpToRemove = null; foreach (TabPage tp in this.tbCtrlpDesigner.TabPages) { if (tp.Name == dsRootComponentName) { tpToRemove = tp; break; } } if (null != tpToRemove) { this.tbCtrlpDesigner.TabPages.Remove(tpToRemove); } //- now remove the DesignSurface this.DesignSurfaceManager.DeleteDesignSurfaceUView(surfaceToErase); //- finally the DesignSurfaceManager remove the DesignSurface //- AND set as active DesignSurface the last one //- therefore we set as active the last TabPage this.tbCtrlpDesigner.SelectedIndex = this.tbCtrlpDesigner.TabPages.Count - 1; } catch (Exception exx) { Debug.WriteLine(exx.Message); if (null != exx.InnerException) { Debug.WriteLine(exx.InnerException.Message); } throw; } }
private bool InitSurfaceText(DesignSurfaceUView surface, Control rootComponent, string text) { Control view = surface.GetView(); if (null == view) { return(false); } PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(view); //- Sets a PropertyDescriptor to the specific property PropertyDescriptor pdS = pdc.Find("Text", false); if (null != pdS) { pdS.SetValue(rootComponent, text); } return(true); }
//- Create the DesignSurface and the rootComponent (a .NET Control) //- using IDesignSurfaceBase.CreateRootComponent() //- if the alignmentMode doesn't use the GRID, then the gridSize param is ignored //- Note: //- the generics param is used to know which type of control to use as RootComponent //- TT is requested to be derived from .NET Control class public DesignSurfaceUView AddDesignSurface <TT>(int width, int height, Interface.AlignmentModeEnum alignmentMode) where TT : Control { const string _signature_ = _name + @"::AddDesignSurface<>()"; if (!this) { throw new Exception(_signature_ + " - Exception: " + _name + " is not initialized! Please set the Property: IUDesigner::Toolbox before calling any methods!"); } //- step.0 //- create a DesignSurface DesignSurfaceUView surface = DesignSurfaceManager.CreateDesignSurfaceEx() as DesignSurfaceUView; this.DesignSurfaceManager.ActiveDesignSurface = surface; //- step.1 //- choose an alignment mode... surface.UseOptionService(alignmentMode); //- step.2 //- enable the UndoEngine (surface.GetService(typeof(UndoEngine)) as UndoEngine).Enabled = true; //- step.3 //- Select the service IToolboxService //- and hook it to our ListBox var tbox = surface.GetService(typeof(IToolboxService)) as ToolboxServiceImp; //- we don't check if Toolbox is null because the very first check: if(!this)... if (null != tbox) { tbox.CtrlToolbox = this.CtrlToolbox; } //- step.4 //- create the Root compoment, in these cases a Form Control rootComponent = null; //- cast to .NET Control because the TT object //- has a constraint: to be a ".NET Control" rootComponent = surface.CreateRootComponent(typeof(TT), DesignSurfaceManager.GetValidName(typeof(TT).Name), new Size(width, height)) as Control; //- rename the Sited component //- (because the user may add more then one Form //- and every new Form will be called "Form1" //- if we don't set its Name) //rootComponent.Site.Name = this.DesignSurfaceManager.GetValidName(typeof(TT).Name); //- step.5 //- enable the Drag&Drop on RootComponent surface.EnableDragandDrop(); //- step.6 //- IComponentChangeService is marked as Non replaceable service IComponentChangeService componentChangeService = (IComponentChangeService)(surface.GetService(typeof(IComponentChangeService))); if (null != componentChangeService) { //- the Type "ComponentEventHandler Delegate" Represents the method that will //- handle the ComponentAdding, ComponentAdded, ComponentRemoving, and ComponentRemoved //- events raised for component-level events componentChangeService.ComponentChanged += (Object sender, ComponentChangedEventArgs e) => { // do nothing }; componentChangeService.ComponentAdded += (Object sender, ComponentEventArgs e) => { DesignSurfaceManager.UpdatePropertyGridHost(surface); }; componentChangeService.ComponentRemoved += (Object sender, ComponentEventArgs e) => { DesignSurfaceManager.UpdatePropertyGridHost(surface); }; } //- step.7 //- now set the Form::Text Property //- (because it will be an empty string //- if we don't set it) if (!InitSurfaceText(surface, rootComponent, rootComponent.Site.Name + " (design mode)")) { return(null); } //- step.8 //- display the DesignSurface AddSurfaceToNewTabPage(surface, rootComponent.Site.Name); //- step.9 //- finally return the DesignSurface created to let it be modified again by user DesignSurfaceManager.UpdatePropertyGridHost(surface); return(surface); }