Macro to hide all objects within a group

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
open_mike
Posts: 15
Joined: Mon Feb 05, 2024 11:22 pm

Macro to hide all objects within a group

Post by open_mike »

Hello,

I am rebuilding a DB I had in Access onto LO Base. I have learned a lot, but my search for making all objects within a group invisible by the click of a button have come up empty. The most I have learned is that it is possible.

In my testing, I have made a macro that makes invisible all types of objects; and it works.
I have grouped them all together and named the group grpDataEntry

Can anyone tell me how to alter this macro in order to make all objects in the group vanish?

Much appreciated:

Group name: grpDataEntry
Button Name: PushButton

Macro

Code: Select all

Sub Main
    Dim Doc As Object
    Dim Form As Object
    Dim Button As Object
    Dim ButtonCtrl As Object
    Dim TextBox As Object
    Dim TextBoxCtrl As Object
    
    ' Get the current document
    Doc = ThisComponent
    
    ' Get the form from the document
    Form = Doc.DrawPage.Forms.getByIndex(0)
    
    ' Get the button from the form
    Button = Form.getByName("PushButton")

    
        ' Get the button control
        ButtonCtrl = Doc.CurrentController.getControl(Button)
        
        ' Get the text box from the form
        TextBox1 = Form.getByName("Text Box 1")
        TextBox2 = Form.getByName("Text Box 2")
        CheckBox1 = Form.getByName("Check Box 1")
        FromattedBox1 = Form.getByName("Formatted Field 1")
        Option1 = Form.getByName("Option Button 1")
        ListBox1 = Form.getByName("List Box 1")
        ComboBox1 = Form.getByName("Combo Box 1")
        LabelBox1 = Form.getByName("Label Field 1")
            
        ' Get the text box control
        TextBoxCtrl = Doc.CurrentController.getControl(TextBox1)
        TextBoxCtrl2 = Doc.CurrentController.getControl(TextBox2)
        CheckBoxCtrl = Doc.CurrentController.getControl(CheckBox1)
        FromattedBoxCtrl = Doc.CurrentController.getControl(FromattedBox1)
        OptionCtrl = Doc.CurrentController.getControl(Option1)
        ListBoxCtrl = Doc.CurrentController.getControl(ListBox1)
        ComboBoxCtrl = Doc.CurrentController.getControl(ComboBox1)
        LabelBoxCtrl = Doc.CurrentController.getControl(LabelBox1)
                
        ' Hide the text box
        TextBoxCtrl.setVisible(False)
        TextBoxCtrl2.setVisible(False)
        CheckBoxCtrl.setVisible(False)
        FromattedBoxCtrl.setVisible(False)
        OptionCtrl.setVisible(False)
        ListBoxCtrl.setVisible(False)
        ComboBoxCtrl.setVisible(False)
        LabelBoxCtrl.setVisible(False)

End Sub
Open Office 4.1.15
Windows 11
User avatar
keme
Volunteer
Posts: 3705
Joined: Wed Nov 28, 2007 10:27 am
Location: Egersund, Norway

Re: Macro to hide all objects within a group

Post by keme »

I don't have a macro/pushbutton solution for you, but depending on your specific task at hand, you may be helped by layers.

See the attached file for an immediate visual. Shift-click the Peekaboo layer tab will hide some content.
 Edit: Please disregard my stupidity! Coming from a Draw question, I seem to have been blinded by the "Object"/"group" terminology to entirely overlook the fact that you are asking about Base and form objects. Sorry! 
Attachments
peekaboo.odg
(9.62 KiB) Downloaded 29 times
Last edited by keme on Mon Mar 04, 2024 7:33 pm, edited 2 times in total.
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Macro to hide all objects within a group

Post by Lupp »

I don't need the trick myself, but it was somehow interesting.
See attached demo. It was created with a recent LibreOffice (7.6 or 24.2) and needed a few changes to also run
in AOO 4.1.7. The code pasted into the first page of the Writer doc does not contain these changes.
aoo111317HideShapeGroupsIncludingControls.odt
(21.69 KiB) Downloaded 38 times
The "large" Group named "testGroupSh" also contains a nested group and a shape without a FormControl. The relevant Subs would also find its way through deeply nested groups.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to hide all objects within a group

Post by JeJe »

Looking at Lupp's document in MRI in OO the group is the first index in the drawpage and can be hidden simply with this

Code: Select all

thiscomponent.drawpage.getbyindex(0).Visible = false
Edit: assumes you know the index though.

Lupp's code worked for me in OO once I overcame my lack of observation and realised (it does say), to hide, this line has to be modified to

Code: Select all

Const visiDefault = false
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
open_mike
Posts: 15
Joined: Mon Feb 05, 2024 11:22 pm

Re: Macro to hide all objects within a group

Post by open_mike »

Hi guys thanks for the replies,
Lupe, I tried your example but nothing happens when I press Push Button

JeJe, I changed Lupe's code to Const visiDefault = false and nothing still happens when I press Push Button.

Is it possible to simplify this? Just 2 or 3 objects in only 1 group (not nested) and no if statements? Just a basic, push the button and they disappear?
This will help me improve.

Thanks again

Mike
Open Office 4.1.15
Windows 11
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to hide all objects within a group

Post by JeJe »

Lupp's code works for me in LO as well. The simplest version going from the name (testGroupSh in Lupp's document) would be something like this:

Code: Select all

dim comp,i
comp = thiscomponent
for i = 0 to comp.drawpage.getcount -1
with comp.drawpage.getbyindex(i)
if .name = "testGroupSh" then .Visible = false: exit for
end with
next
Edit, or trimmed a little more.

Code: Select all

dim i
with   thiscomponent.drawpage
for i = 0 to .getcount -1
if .getbyindex(i).name = "testGroupSh" then .getbyindex(i).Visible = false: exit for
next
end with
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to hide all objects within a group

Post by JeJe »

Is your security set to allow Lupp's document macros to run?
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
open_mike
Posts: 15
Joined: Mon Feb 05, 2024 11:22 pm

Re: Macro to hide all objects within a group

Post by open_mike »

I appreciate the trimmed down version but for loops make it more complicated. I am looking for the basic command and format of the command.

Let's try this another way. The code I gave you was from practice code. I applied it to my actual database in a way I thought would work.
Here is the actual macro I am trying to get to work.
Can you tell me why this macro doesn't work?

Code: Select all

REM  *****  BASIC  *****

Sub btnAmenitiesPressed
    Dim Doc As Object
    Dim Form As Object
    Dim Button As Object
    Dim ButtonCtrl As Object
    Dim GroupBox As Object
    Dim GroupBoxCtrl As Object
    
    ' Get the current document
    Doc = ThisComponent
    
    ' Get the form from the document
    Form = Doc.DrawPage.Forms.getByIndex(0)
    
    ' Get the button from the form
    Button = Form.getByName("btnAmenities")

    
        ' Get the button control
        ButtonCtrl = Doc.CurrentController.getControl(Button)
        
        ' Get the Group box from the form
        GroupBox = Form.getByName("GroupBox")
            
        ' Get the Group box control
        GroupBoxCtrl = Doc.CurrentController.getControl(GroupBox)
                
        ' Hide the text box
        GroupBoxCtrl.setVisible(False)

End Sub
When I click the button I get this error:
Basic runtime error:
An eception occured
Type: com.sun.star.container.NoSuchElementException
Message: ./forms/source/misc/InterfaceContainer.cxx:696
Last edited by robleyd on Tue Mar 05, 2024 12:28 am, edited 1 time in total.
Reason: Add CODE tags
Open Office 4.1.15
Windows 11
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to hide all objects within a group

Post by JeJe »

I suggest MRI and learn how to inspect objects and you can examine it yourself.

viewtopic.php?t=49294

You don't say the line the error occurred. If its this one it could be because the group isn't a control and that's what Form.getByName gets.

Code: Select all

GroupBox = Form.getByName("GroupBox")
MRI is how you can examine that.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to hide all objects within a group

Post by JeJe »

In my first post I gave you a one line way - provided you know the index.

The reason a loop is needed with the name is because get by name isn't available, just get by index there.

You can simplify getting by name to a one line call by putting the loop in separate function as Lupp has done, and that function out of the way in a separate module if you want so you never have to see it again.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
open_mike
Posts: 15
Joined: Mon Feb 05, 2024 11:22 pm

Re: Macro to hide all objects within a group

Post by open_mike »

JeJe,
sorry about that. I completely forgot to look at the line.
Yes it was GroupBox = Form.getByName("GroupBox")

I changed it to GroupBox = Form.getByName("grpDataEntry")
now when I press the button only one label field disappears.

Huh...
I just noticed something.
When I control click that label to get the name, the name changed to grpDataEntry. In fact all the objects names changed to grpDataEntry.
This make sense to me since they are part of the group.
If they all have the same name, shouldn't they all disappear?

Thank you for the info on MRI. I have seen references to MRI on many posts but didn't know what it was.
I read the intro and documentation in the links from the page. I have a general idea what it does but I think I need to hunt down more tutorials before I can figure out how to use it.

As with previous languages I've learned, I am sure I will get to if statements anf for loops once I grow on these macros. But for now it's too soon for that. I need to get the basics down.
Open Office 4.1.15
Windows 11
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to hide all objects within a group

Post by JeJe »

MRI is easy, you install it, make sure the library is loaded so it can be used then you type somewhere in your code,

Code: Select all

MRI whateverobjectname
and up pops a window showing all the methods and properties available for your object. I find it indespensible.


Edit, this also:

https://extensions.openoffice.org/en/pr ... -ide-tools
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
open_mike
Posts: 15
Joined: Mon Feb 05, 2024 11:22 pm

Re: Macro to hide all objects within a group

Post by open_mike »

You know, I installed the MRI extension but should I also install the MRI - UNO Object Inspection Tool listed below?

Any thought why only one label becomes invisible?
Open Office 4.1.15
Windows 11
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to hide all objects within a group

Post by JeJe »

I don't understand your first sentence there - there is only one MRI.

If controls have the same name and you use get by name then I presume you get the first control with that name.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
open_mike
Posts: 15
Joined: Mon Feb 05, 2024 11:22 pm

Re: Macro to hide all objects within a group

Post by open_mike »

Thanks for the reply,

I had the same thought - That it hides the first object with that name but that isn't the case. The lable it hides is right in the middle.

I did some testing to try to figure it out.
The label that disappears is the 37th object. For a visual here is what I have

City: <City text box>
Legal Description: <Legal Decription text box>

To see what happens I deleted the label Legal Description.
I hit the button and the only object to disappear was <City text box>

Next, I deleted <City text box> and hit the button
and the only object to disappear was The City label

I see the pattrn here but I don't understand it
Open Office 4.1.15
Windows 11
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to hide all objects within a group

Post by JeJe »

Controls have a tabindex so maybe its that order.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
open_mike
Posts: 15
Joined: Mon Feb 05, 2024 11:22 pm

Re: Macro to hide all objects within a group

Post by open_mike »

I just checked. The labels dont have a tab index, however all the text boxes and buttons have a tab index = 0
Open Office 4.1.15
Windows 11
Post Reply