Need help for creating a simple checklist box

Hello

import rhinoscriptsyntax as  rs
layers = rs.LayerNames()
if layers:
    items  = [(layer, rs.IsLayerOn(layer)) for layer in layers]
    results  = rs.CheckListBox(items, "Turn layers on/off", "Layers")
    if results:
        for  layer, state in results: rs.LayerVisible(layer, state)

How can i change “layers” with my own desired fields and how can i store the checked items in my file?

Hi @anycubic_photon,

the documentation says rs.CheckListBox requires a list of tuples as ‘items’ argument. the results will again be a list of tuples.

there are several ways to store the results in your file, (e.g. rs.SetDocumentData, sticky values). How do you expect to use the data after storing it?

cheers,
Tim

import rhinoscriptsyntax as rs

items = [('val1', True), ('Val2', False)]

results  = rs.CheckListBox(items, "Show my check options", "Layers")
if results:
    # print results   
    for result in results:
        print result[0], result[1]
1 Like

Thank you so much
my first problem is solved
i mean i want to store checked items in my file (when im checked one box next time i run the script it showed the checked

i just want the store checked or unchecked in my file not any additional data

you can use sticky values for that (scriptcontext.sticky) for example:

checkbox.Checked = (sc.sticky["checked"] if sc.sticky.has_key("checked") else False)

and then in your checked event set:

sc.sticky["checked"]  = checkbox.Checked

Thank you
im a little newbie in scripting . can you please tell me where of the code should i put this?

Sorry I was assuming you were using an Eto form.

In your case you need to put where you have your True and False values the actual value of the sticky.
Not behind a pc right now but if you click on the checkbox and it returns a value, set the sticky to that value.

i will appreciate it if you can give me the exact code that should i use

I’m actually not quite sure what you are trying to achieve but here is an example of how you could use a sticky to store a layer state once. After you run the script the second time, it will read the previously stored state and set the checkboxes accordingly.

import rhinoscriptsyntax as  rs
import scriptcontext as sc

if sc.sticky.has_key("layerstate"):
    items = sc.sticky["layerstate"]
else:    
    layers = rs.LayerNames()
    if layers:
        items = []
        for layer in layers:
            is_on = rs.IsLayerOn(layer)
            items.append((layer, is_on))
        sc.sticky["layerstate"] = items
results  = rs.CheckListBox(items, "Turn layers on/off", "Layers")
if results:
    for  layer, state in results:
        if rs.IsLayer(layer):
            rs.LayerVisible(layer, state)
1 Like

import rhinoscriptsyntax as rs

items = [(‘val1’, True), (‘Val2’, False)]

results = rs.CheckListBox(items, “Show my check options”, “Layers”)
if results:
# print results
for result in results:
print result[0], result[1]

This one if working very good for my purpose . its just need the store boxes that are checked or uncheked