libreofficelibreoffice-basiclibreoffice-base

Libreoffice Base macro - How to make visible/hide a control on a form?


I created a database Form with 2 comboboxes, Combo1 and Combo2. Combo2 visibility depends on Combo1 status: Combo1 checked, Combo2 visible; Combo1 unchecked, Combo2 hidden.

I expected to find something like Combo2.setVisible(Combo1.isVisible()) but I have been too optimist.

This is the macro I have been playing with:

Sub MyMacro(oEvent as Object)
    msgbox(oEvent.Source.getState())   rem returns Checked/Unchecked status of Combo1
    msgbox(oEvent.source.model.Name)   rem Name of the Event generator (Combo1)
End Sub
cb2 = ThisComponent.Drawpage.Forms.getByName("MainForm").getByName("Combo2")
msgbox(cb2.Name)

prints correctly Combo2, so I assume I have a proper reference...

And now?

Today is the very first day I play with Libreoffice Base (and its bugs), and this is just something nice that I would like to add for tomorrow's demo. I NEVER used before a macro both in Microsoft nor Libreoffice suites, so I am moving veeery slow.

Last time I used BASIC was probably around 1990.

I went through a number of tutorials online (usually based on Libreoffice Calc cells), string-searched both main books by Andrew Pitonyak ("Useful Macro Information For OpenOffice.org" and "OpenOffice.org Macros Explained (OOME)") but after trying tons of snippets I got nowhere.

Same for libreoffice macro - toggle enablevisible on a textfield

Any pointer?


Solution

  • This is a working snippet (not guaranteed to be the most straightforward, as my experience with macros is one day long):

    Sub SetVisibility(oEvent as Object)
        cb2 = ThisComponent.Drawpage.Forms.getByName("MainForm").getByName("Combo2")
        cb2.enableVisible = oEvent.Source.getState()   
        rem the action is activated by Combo1, the event source
    End Sub