As indicated in the subject, I use a macro to create one or more buttons in a SpreadSheet.
If the user clicks on it, a certain “update” should be carried out in the sheet and the button should then “disappear” again
The button is bound to the sub ‘BtnFunc’.
However, the line “form.removeByName( model.Name )” apparently only deletes the event (after that, the button no longer reacts to a click).
The “representation” remains in the sheet…
So I assume that I somehow have to delete the other two objects (btn & shape).
So far I haven’t found anything about it.
Where (in which container) are they?
I gave the objects extra unique names (btn = cell.AbsoluteName & “_BTN” & shape.Name = cell.AbsoluteName & “_SHAPE”).
Xray shows “…_BTN” as the model.Name, but I can’t find “…_SHAPE”.
How do I get they ?
Public Sub PlaceBtnOnCell( action As String, form As Object, cell As Object, btnTxt As String, Optional backColor As Long, Optional tag As String ) Dim btn As Object: btn = ThisComponent.createInstance( "com.sun.star.form.component.CommandButton" ) Dim shape As Object: shape = ThisComponent.createInstance( "com.sun.star.drawing.ControlShape" ) Dim script As Object: script = new com.sun.star.script.ScriptEventDescriptor Dim sheet As Object: sheet = cell.Spreadsheet btn.Name = cell.AbsoluteName & "_BTN" btn.Label = btnTxt btn.FocusOnClick = False If IsMissing( tag ) Then tag = cell.AbsoluteName & "_TAG" btn.Tag = tag shape.Name = cell.AbsoluteName & "_SHAPE" shape.Control = btn sheet.Drawpage.add( shape ) shape.Anchor = cell shape.Size = GetSizeOfCellRange( cell ) shape.Position = cell.Position shape.SizeProtect = True shape.MoveProtect = True If IsMissing( backColor ) Then backColor = cell.CellBackColor shape.ControlBackground = backColor shape.CharColor = cell.CharColor shape.CharFontName = cell.CharFontName shape.CharHeight = cell.CharHeight shape.CharWeight = cell.CharWeight script.ListenerType = "com.sun.star.awt.XActionListener" script.EventMethod = "actionPerformed" script.ScriptType = "StarBasic" script.ScriptCode = action form.registerScriptEvent( form.count - 1, script ) End Sub
Sub BtnFunc( event As Variant ) RemoveButton( event.Source.Model ) End Sub Sub RemoveButton( model As Object ) 'Xray model Dim form As Object: form = model.Parent ' removes: form.registerScriptEvent( form.count - 1, script ) 'Done in PlaceBtnOnCell 'form.removeByName( model.Name ) ' How to: ' get/remove shape ? ' get/remove btn ? End Sub
[erAck: edited to format as code]