Example #1
0
        public ExcelButton(ExcelInterop.Range range, ExcelButtonDefinition definition)
        {
            OwnerRange        = range;
            OwnerRange.Value2 = null;
            ExcelInterop.Worksheet  worksheet  = null;
            ExcelInterop.OLEObjects oleObjects = null;
            ExcelInterop.OLEObject  obj        = null;

            try
            {
                worksheet = OwnerRange.Worksheet;
                Name      = $"ExcelBtn{Interlocked.Increment(ref cpt)}";

                oleObjects = worksheet.OLEObjects();
                obj        = oleObjects.Add("Forms.CommandButton.1",
                                            Type.Missing,
                                            false,
                                            false,
                                            Type.Missing,
                                            Type.Missing,
                                            Type.Missing,
                                            OwnerRange.Left + definition.X,
                                            OwnerRange.Top + definition.Y,
                                            definition.W == 0 ? OwnerRange.Width : definition.W,
                                            definition.H == 0 ? OwnerRange.Height : definition.H);

                obj.Name = Name;
                object s = worksheet.GetType().InvokeMember(Name, BindingFlags.Default | BindingFlags.GetProperty, null, worksheet, null);
                commandButton           = s as ExcelForms.CommandButton;
                commandButton.FontName  = "Arial";
                commandButton.Font.Size = 8;
                commandButton.Caption   = definition.Label;
                //if (excelTemplateDefinition.W == 0 && excelTemplateDefinition.H == 0)
                //    commandButton.AutoSize = true;
                obj.Placement = ExcelInterop.XlPlacement.xlMove;
            }
            finally
            {
                if (obj != null)
                {
                    ExcelApplication.ReleaseComObject(obj);
                }
                if (oleObjects != null)
                {
                    ExcelApplication.ReleaseComObject(oleObjects);
                }
                if (worksheet != null)
                {
                    ExcelApplication.ReleaseComObject(worksheet);
                }
                obj        = null;
                oleObjects = null;
                worksheet  = null;
            }
        }
 private ExcelBindingDefinitionButton(BindingDefinitionDescription definitionDescription, ExcelTemplateDefinition templateDefinition, ExcelButtonDefinition definition)
     : base(definitionDescription)
 {
     TemplateDefinition = templateDefinition;
     Definition         = definition;
     if (!string.IsNullOrEmpty(definition.Label))
     {
         LabelBindingDefinition = BindingDefinitionFactory.CreateInstances(templateDefinition, DefinitionDescription);
         CanNotify = LabelBindingDefinition.CanNotify;
     }
     RetrieveOnClickMethod();
     RetrieveEnableProperty();
 }
        public static ExcelBindingDefinitionButton CreateInstance(ExcelTemplateDefinition templateDefinition, string definition)
        {
            ExcelBindingDefinitionButton ret = null;

            if (!string.IsNullOrEmpty(definition))
            {
                try
                {
                    ExcelButtonDefinition        excelButtonDefinition = definition.Deserialize <ExcelButtonDefinition>();
                    BindingDefinitionDescription definitionDescription = BindingDefinitionDescription.CreateBindingDescription(templateDefinition, excelButtonDefinition.Label, excelButtonDefinition.Label);
                    ret = new ExcelBindingDefinitionButton(definitionDescription, templateDefinition, excelButtonDefinition);
                }
                catch (Exception ex)
                {
                    string message = $"Cannot retrieve the button dataAccessor '{definition.EmptyIfNull()}'. {ex.Message}";
                    throw new EtkException(message);
                }
            }
            return(ret);
        }