public void Dispose() { if (commandButton != null) { IsDisposed = true; if (CurrentCommand != null) { commandButton.Click -= CurrentCommand; } ExcelInterop.Worksheet worksheet = OwnerRange.Worksheet; ExcelInterop.OLEObject obj = worksheet.OLEObjects(Name); obj.Delete(); ExcelApplication.ReleaseComObject(obj); ExcelApplication.ReleaseComObject(commandButton); ExcelApplication.ReleaseComObject(OwnerRange); ExcelApplication.ReleaseComObject(worksheet); obj = null; commandButton = null; OwnerRange = null; worksheet = null; } }
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; } }
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; } }
/// <summary> /// Returns the control with the specified name in the specified worksheet. /// </summary> /// <param name='name'>Name of the control to find.</param> /// <param name='sheet'>Worksheet object that contains the control.</param> /// <returns> /// Returns the specified control, or null if it is not found. /// </returns> object FindControl(string name, Excel.Worksheet sheet) { Excel.OLEObject theObject; try { theObject = (Excel.OLEObject)sheet.OLEObjects(name); return(theObject.Object); } catch { // Returns null if the control is not found. } return(null); }
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; } }