loopsvariableslivecode

Livecode: Change control properties using a loop


I have a card with a large number of controls on it: 33 buttons, 33 menus, and 33 radio button pairs. Each time a control is used a property of the control changes -- the hilite on one of the radio buttons in each pair is activated, a menu item appears other than the original label (which is 0), and the buttons change color from their default. I want to write a loop that resets all the controls to their default state. The buttons have been labeled RB01 through RB33, the menu items are labeled RM01 through RM33, and the radio button pairs are RR01 through RR33. Also, the ID numbers of the controls are not consecutive. Here's what I have so far:

    on mouseUp
       repeat with x = 1 to 33
          set the backgroundColor of button "RB[x]" to default
          set the Label of button "RM[x]" to 0
          set the highlite of of group "RR[x]" to FALSE
       end repeat
    end mouseUp

The use of "RB[x]" is the problem here and I haven't been able to find anything a good solution despite searching. Is there a way to do this in Livecode or is there a better naming convention for the controls so I could do this in a loop?


Solution

  • Your solution is very close. The problem is with the use of the [ ] notation, which is reserved for arrays. Use the concatenation operator & instead. Notice that I'm also padding the x with a leading zero if needed. The ( ) forces the concatenated string to be evaluated before the object name.

    on mouseUp
       repeat with x = 1 to 33
          if length(x) < 2 then put "0" before x
          set the backgroundColor of button ("RB" & x) to default
          set the Label of button ("RB" & x) to 0
          set the highlite of of group ("RB" & x) to FALSE
       end repeat
    end mouseUp