Page 1 of 1

Macro to hide all objects within a group

Posted: Mon Mar 04, 2024 1:18 am
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

Re: Macro to hide all objects within a group

Posted: Mon Mar 04, 2024 3:26 pm
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! 

Re: Macro to hide all objects within a group

Posted: Mon Mar 04, 2024 7:02 pm
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 123 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.

Re: Macro to hide all objects within a group

Posted: Mon Mar 04, 2024 7:28 pm
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

Re: Macro to hide all objects within a group

Posted: Mon Mar 04, 2024 8:08 pm
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

Re: Macro to hide all objects within a group

Posted: Mon Mar 04, 2024 8:23 pm
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

Re: Macro to hide all objects within a group

Posted: Mon Mar 04, 2024 8:48 pm
by JeJe
Is your security set to allow Lupp's document macros to run?

Re: Macro to hide all objects within a group

Posted: Mon Mar 04, 2024 11:05 pm
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

Re: Macro to hide all objects within a group

Posted: Mon Mar 04, 2024 11:40 pm
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.

Re: Macro to hide all objects within a group

Posted: Mon Mar 04, 2024 11:46 pm
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.

Re: Macro to hide all objects within a group

Posted: Tue Mar 05, 2024 12:55 am
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.

Re: Macro to hide all objects within a group

Posted: Tue Mar 05, 2024 1:03 am
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

Re: Macro to hide all objects within a group

Posted: Tue Mar 05, 2024 1:08 am
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?

Re: Macro to hide all objects within a group

Posted: Tue Mar 05, 2024 2:55 am
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.

Re: Macro to hide all objects within a group

Posted: Tue Mar 05, 2024 8:19 pm
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

Re: Macro to hide all objects within a group

Posted: Tue Mar 05, 2024 9:26 pm
by JeJe
Controls have a tabindex so maybe its that order.

Re: Macro to hide all objects within a group

Posted: Wed Mar 06, 2024 3:50 am
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