[Solved] How to delete buttons at runtime (Calc)

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Davte
Posts: 16
Joined: Sat Sep 10, 2016 11:29 pm
Location: Italy

[Solved] How to delete buttons at runtime (Calc)

Post by Davte »

I'm looking for the code to delete a button at runtime.
I tried adjusting this code, but it doesn't work (maybe it's Draw-Form-Specific, since the method "RemoveByName" is not supported in Calc dialogs) and gives me an error.
Here is the loop in which the *code to delete button* should fit:

Code: Select all

REM Previously defined: Elemento() as array containing the final part of the name of each control (the complete name is {"btn" & Elemento(i)}).
REM Previously defined: frmArchivio is my Dialog
Dim i As Integer, oButton As Object
		For i=0 To UBound(Elemento)
			oButton = frmArchivio.GetControl("btn" & Elemento(i))
			'oButton.SetVisible(False)     REM This is what I'm using now, it works for the eye but not for the mind, so I'd like to replace it with *code to delete button*
		Next i
When I tried "oButton.Dispose" (and something like "model.dispose" I found somewhere but didn't work) it seemed to work but gave me several errors like "missing argument".
I guess it may have something to do with the "trialism" of each control (model, shape, view), but can't figure out how to solve it.
Thank you in advance, I'm learning much on this forum but still have a lot to learn,
Davide



SOLUTION:
Remove any Listener before disposing.

Code: Select all

			oButton = frmArchivio.GetControl(bName)
			oButton.removeActionListener(ElementListener)
			oButton.Dispose
Last edited by Davte on Sat Sep 24, 2016 1:10 am, edited 1 time in total.
OpenOffice 4.1.2 on Windows 10
User avatar
Zizi64
Volunteer
Posts: 11360
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: How to delete buttons at runtime (Calc)

Post by Zizi64 »

try this:

Code: Select all

Form.Model.removeByName("Push Button 1")
see this thread:
viewtopic.php?f=20&t=78534

Or please upload your example .ods file with the full macro subroutine.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Davte
Posts: 16
Joined: Sat Sep 10, 2016 11:29 pm
Location: Italy

Re: How to delete buttons at runtime (Calc)

Post by Davte »

Form.Model.RemoveByName("btn" & Elemento(i)) gives an error (run-time error raise_exception occurred errorType: com.sun.star.container.NoSuchElementException Message: .).
The thread you linked has buttons in sheet, not in a dialog, I tried that code but it raises the same error in the dialog (whereas works perfectly in sheet).
OpenOffice 4.1.2 on Windows 10
User avatar
Zizi64
Volunteer
Posts: 11360
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: How to delete buttons at runtime (Calc)

Post by Zizi64 »

Please upload your example file with the dialog and macro code.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Davte
Posts: 16
Joined: Sat Sep 10, 2016 11:29 pm
Location: Italy

Re: How to delete buttons at runtime (Calc)

Post by Davte »

Ok, but it's a bit tricky.
- Open the file: the auto-run macro will create a folder "Files" where the file is stored.
- Create some folder inside "files" (at least one), and at least 1 file inside that folder you created.
- Enter the username (Admin)
- When you click on a button, it should delete all buttons and create new ones with the name of the content of that folder. With "setvisible(False)" it works, but not with frmArchivio.Model.RemoveByName(bName). Clicking the button raises the error.
Sorry about the trickiness, that's why I was reluctant about uploading it :\
Attachments
Gestione laboratorio.ods
(20.42 KiB) Downloaded 258 times
OpenOffice 4.1.2 on Windows 10
Davte
Posts: 16
Joined: Sat Sep 10, 2016 11:29 pm
Location: Italy

Re: How to delete buttons at runtime (Calc)

Post by Davte »

(Maybe) Solved:
Working on the code, I noticed that the "oButton.Dispose" works if the listener has been previously removed. Code working:

Code: Select all

	Dim i As Integer, oButton As Object, bName As String, oModel As Object
		For i=0 To UBound(Elemento)
			bName = "btn" & Elemento(i)
			oButton = frmArchivio.GetControl(bName)
			oButton.removeActionListener(ElementListener)
			oButton.Dispose
		Next i
I'll wait for eventual answers from more expert users, but I guess this way it can work.
OpenOffice 4.1.2 on Windows 10
Post Reply