public ReportControlsStructure() { reportControlProperty = new ReportControlProperties(); }
public Control CreateControl(ReportUtils.ReportControlProperties reportControlProperties) { try { //- step.1 //- get the IDesignerHost //- if we are not able to get it //- then rollback (return without do nothing) IDesignerHost host = GetIDesignerHost(); if (null == host) { return(null); } //- check if the root component has already been set //- if not so then rollback (return without do nothing) if (null == host.RootComponent) { return(null); } //- //- //- step.2 //- create a new component and initialize it via its designer //- if the component has not a designer //- then rollback (return without do nothing) //- else do the initialization //IComponent newComp = host.CreateComponent(typeof(ReportUtils.Type)); //IComponent newComp = host.CreateComponent(Type.GetType(reportControlProperties.Type)); IComponent newComp = host.CreateComponent(GetTypeFromSimpleName(reportControlProperties.Type)); Type type = GetTypeFromSimpleName(reportControlProperties.Type); if (null == newComp) { return(null); } IDesigner designer = host.GetDesigner(newComp); if (null == designer) { return(null); } if (designer is IComponentInitializer) { ((IComponentInitializer)designer).InitializeNewComponent(null); } //- //- //- step.3 //- try to modify the Size/Location of the object just created PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(newComp); //- Sets a PropertyDescriptor to the specific property. PropertyDescriptor pdS = pdc.Find("Size", false); if (null != pdS) { pdS.SetValue(newComp, (reportControlProperties.Size)); } PropertyDescriptor pdName = pdc.Find("Name", false); if (null != pdName) { pdName.SetValue(newComp, (reportControlProperties.Name)); } PropertyDescriptor pdL = pdc.Find("Location", false); if (null != pdL) { pdL.SetValue(newComp, new Point(reportControlProperties.Location._X, reportControlProperties.Location._Y)); } PropertyDescriptor pdF = pdc.Find("Font", false); if (null != pdF) { Font font = new Font(reportControlProperties.Font.FontFamily, reportControlProperties.Font.FontSize, reportControlProperties.Font.FontStyle); pdF.SetValue(newComp, font); } PropertyDescriptor pdFc = pdc.Find("ForeColor", false); if (null != pdFc) { Color color = Color.FromName(reportControlProperties.Font.FontColor); if (!color.IsKnownColor) { color = ColorTranslator.FromHtml("#" + color.Name); } pdFc.SetValue(newComp, color); } PropertyDescriptor pdFBorder = pdc.Find("BorderStyle", false); if (null != pdFBorder) { if (reportControlProperties.Border) { pdFBorder.SetValue(newComp, BorderStyle.FixedSingle); } else { pdFBorder.SetValue(newComp, BorderStyle.None); } } PropertyDescriptor pdText = pdc.Find("Text", false); if (null != pdText) { pdText.SetValue(newComp, reportControlProperties.Text); } if (type == typeof(PictureBox)) { PropertyDescriptor pdImageLocation = pdc.Find("ImageLocation", false); if (null != pdImageLocation) { pdImageLocation.SetValue(newComp, reportControlProperties.ImageName); } pdImageLocation = pdc.Find("SizeMode", false); if (null != pdImageLocation) { pdImageLocation.SetValue(newComp, PictureBoxSizeMode.Zoom); } } PropertyDescriptor pdMultiLine = pdc.Find("Multiline", false); if (null != pdMultiLine) { pdMultiLine.SetValue(newComp, reportControlProperties.MultiLine); } PropertyDescriptor pdTextAlign = pdc.Find("TextAlign", false); if (null != pdTextAlign) { if (type == typeof(Label)) { ContentAlignment c = (ContentAlignment)Enum.Parse(typeof(ContentAlignment), reportControlProperties.TextAlign.ToString()); pdTextAlign.SetValue(newComp, c); } } PropertyDescriptor pdTag = pdc.Find("Tag", false); if (null != pdTag) { pdTag.SetValue(newComp, reportControlProperties.Binding); } if (type == typeof(TableLayoutPanel)) { PropertyDescriptor pdTableLayout = pdc.Find("RowCount", false); if (null != pdTableLayout) { pdTableLayout.SetValue(newComp, reportControlProperties.RowsColumns._Rows); } pdTableLayout = pdc.Find("ColumnCount", false); if (null != pdTableLayout) { pdTableLayout.SetValue(newComp, reportControlProperties.RowsColumns._Columns); } pdTableLayout = pdc.Find("CellBorderStyle", false); if (null != pdTableLayout) { if (reportControlProperties.Border) { pdTableLayout.SetValue(newComp, DataGridViewAdvancedCellBorderStyle.Single); } else { pdTableLayout.SetValue(newComp, DataGridViewAdvancedCellBorderStyle.None); } } } if (type == typeof(IVL_ImagePanel)) { PropertyDescriptor pdTableLayout = pdc.Find("Images", false); if (null != pdTableLayout) { pdTableLayout.SetValue(newComp, reportControlProperties.NumberOfImages); } pdTableLayout = pdc.Find("BorderStyle", false); if (null != pdTableLayout) { pdTableLayout.SetValue(newComp, BorderStyle.FixedSingle); } } //- //- //- step.4 //- commit the Creation Operation //- adding the control to the DesignSurface's root component //- and return the control just created to let further initializations ((Control)newComp).Parent = host.RootComponent as Control; return(newComp as Control); }//end_try catch (Exception exx) { Debug.WriteLine(exx.Message); if (null != exx.InnerException) { Debug.WriteLine(exx.InnerException.Message); } throw; }//end_catch }