Beispiel #1
0
        public ExcelCheckBox(ExcelInterop.Range range, ExcelCheckBoxDefinition definition)
        {
            OwnerRange        = range;
            OwnerRange.Value2 = null;
            ExcelInterop.Worksheet  worksheet  = null;
            ExcelInterop.OLEObjects oleObjects = null;
            ExcelInterop.OLEObject  oleObject  = null;
            try
            {
                worksheet = OwnerRange.Worksheet;
                Name      = $"ExcelCB{Interlocked.Increment(ref cpt)}";

                oleObjects = worksheet.OLEObjects();
                oleObject  = oleObjects.Add("Forms.CheckBox.1",
                                            Type.Missing,
                                            true,
                                            false,
                                            Type.Missing,
                                            Type.Missing,
                                            Type.Missing,
                                            OwnerRange.Left + 3,
                                            OwnerRange.Top + 1,
                                            12,
                                            12);
                oleObject.Name      = Name;
                oleObject.Placement = ExcelInterop.XlPlacement.xlMove;
                CheckBox            = worksheet.GetType().InvokeMember(Name, BindingFlags.Default | BindingFlags.GetProperty, null, worksheet, null) as ExcelForms.CheckBox;

                CheckBox.SpecialEffect = ExcelForms.fmButtonEffect.fmButtonEffectSunken;
                CheckBox.TripleState   = false;

                CheckBox.Caption              = string.Empty;
                CheckBox.BackColor            = (int)OwnerRange.Interior.Color;
                CheckBox.BackStyle            = ExcelForms.fmBackStyle.fmBackStyleTransparent;
                oleObject.Interior.ColorIndex = -4142;
                CheckBox.AutoSize             = false;

                oleObject = null;
            }
            finally
            {
                if (oleObject != null)
                {
                    ExcelApplication.ReleaseComObject(oleObject);
                }
                if (oleObjects != null)
                {
                    ExcelApplication.ReleaseComObject(oleObjects);
                }
                if (worksheet != null)
                {
                    ExcelApplication.ReleaseComObject(worksheet);
                }

                oleObject  = null;
                oleObjects = null;
                worksheet  = null;
            }
        }
Beispiel #2
0
        public void Dispose()
        {
            if (CheckBox != null)
            {
                IsDisposed = true;
                if (CurrentOnClick != null)
                {
                    CheckBox.Click -= CurrentOnClick;
                }

                ExcelInterop.Worksheet worksheet = OwnerRange.Worksheet;
                ExcelInterop.OLEObject oleObject = worksheet.OLEObjects(Name);
                oleObject.Delete();

                ExcelApplication.ReleaseComObject(oleObject);
                ExcelApplication.ReleaseComObject(OwnerRange);
                ExcelApplication.ReleaseComObject(worksheet);
                ExcelApplication.ReleaseComObject(CheckBox);
                oleObject  = null;
                OwnerRange = null;
                worksheet  = null;
                CheckBox   = null;
            }
        }