4
$\begingroup$

I am trying to build an UI for my algorithm, to handle some input parameters. Under that scope I am building a checkbox menu.

What I need to do is nest some checkboxes under some parent checkbox, so that when I click "Test", all checkboxes nested bellow can be automatically checked, but then I want to have the ability to unlclick some of them (or parent) individually.

So, if I call them:

Test

-Subtest 1

-Subtest 2

I want to be able to check Test1 and have subtest 1 and subtest 2 checked, but to be able to override them, and unclick one of them, or even unclick Test and keep subtest1 and subtest2 clicked.

Keep in mind that test, subtest1 and suntest2 are referring to 3 individual objects, and test1 is not just a group shortcut.

I wrote this (it does not work)

{"Test 1",Checkbox[Dynamic[yesNoTest]]}
{"--Sub Test 1",
If[TrueQ[Dynamic[yesNoTest]],Checkbox[Dynamic[yesNoTestSub1]],

Checkbox[Dynamic[yesNoTestSub1]]
]}
{"--Sub Test 2",
If[TrueQ[Dynamic[yesNoTest]],Checkbox[Dynamic[yesNoTestSub1]],

Checkbox[Dynamic[yesNoTestSub2]]
]}

I do not know how to set a checkbox being "clicked" by default. My basic idea was to test weather yesNoTest is True, and if so return Checkbox[Dynamic[yesNoTestSub1]] == True, else return it without knowing weather T or F and waiting for user to give input (as it is already)

$\endgroup$
2
  • $\begingroup$ How many sub-checkboxes do you expect to have? $\endgroup$
    – Szabolcs
    Mar 29, 2017 at 8:39
  • $\begingroup$ @Szabolcs Actually there may be a case of 10 or so. Each of them will have a variable like yesNoTestSub1 that will be T/F. At a later on stage, I will use those variables to execute or not some parts of my code. $\endgroup$
    – Fierce82
    Mar 29, 2017 at 8:41

1 Answer 1

5
$\begingroup$

The idea is to use the second argument of Dynamic.

 DynamicModule[{yesNoTest = True
               , yesNoTestSub1 = True
               , yesNoTestSub2 = True},
         Grid @ 
         {
           {"Test 1", Checkbox[ Dynamic[yesNoTest, 
                      (    yesNoTest = #
                         ; If[#, yesNoTestSub1 = yesNoTestSub2 = True]
                      ) &]]
           }
           ,
           {"--Sub Test 1", Checkbox[Dynamic[yesNoTestSub1]]},
           {"--Sub Test 2", Checkbox[Dynamic[yesNoTestSub2]]}
         }
 ]
$\endgroup$
1
  • $\begingroup$ Exactly what I was seeking. Excellent use of # patterns. Still struggling to learn how to use it. Appreciate it. I added a variation in your code to automatically uncheck children boxes too, by adding: If[#, yesNoTestSub1 = yesNoTestSub2 = True, yesNoTestSub1 = yesNoTestSub2 = False] $\endgroup$
    – Fierce82
    Mar 29, 2017 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.