GUI enabling/disabling of controls: Difference between revisions

m (→‎{{header|Perl 6}}: use nicer spacing & title)
Line 1,121:
end if
End Sub</lang>
 
=={{header|LiveCode}}==
In LiveCode, GUIs are expected to be designed using the IDE, with the developer writing scripts to handle messages that are passed by the LC Engine. It is certainly possible to dynamically create windows (stacks) and all the various controls, however it is somewhat antithetical to the LC approach of rapid development.
 
1. Create a new mainstack
 
2. Draw on two buttons
 
2.1 The first button name is "Button1", with a label of "Increment"
 
2.2 The second button name is "Button2", with a label of "Decrement"
 
3. Draw a text field and name it "Field1", enter 0 as its content.
 
4. Select Object->Card Script and enter the following to handle enabling the buttons.
<lang LiveCode>command enableButtons v
switch
case v <= 0
put 0 into fld "Field1"
set the enabled of btn "Button1" to true
set the enabled of btn "Button2" to false
break
case v >= 10
put 10 into fld "Field1"
set the enabled of btn "Button1" to false
set the enabled of btn "Button2" to true
break
default
set the enabled of btn "Button1" to true
set the enabled of btn "Button2" to true
put v into fld "Field1"
end switch
end enableButtons</lang>
 
For the increment button, click on Button 1 (in edit mode) and the select "code" from the toolbar and enter the following to handle clicks<lang LiveCode>on mouseUp
put field "Field1" into x
add 1 to x
enableButtons x
end mouseUp</lang>
 
Repeat for Button 2 (note we are subtracting here)<lang LiveCode>on mouseUp
put field "Field1" into x
subtract 1 from x
enableButtons x
end mouseUp</lang>
 
Finally add the code to handle keyboard entry in text field's code. Note this code prevents entering digits outside 0-9 and limits the value between 0 and 10.<lang LiveCode>on rawKeyDown k
if numToChar(k) is among the items of "1,2,3,4,5,6,7,8,9,0" then
if (numToChar(k) + the text of me) <= 10 then
enableButtons (numToChar(k) + the text of me)
end if
else if k = 65288 then
pass rawKeyDown
end if
end rawKeyDown</lang>
 
Switch back to the form, and enter run mode to execute.
 
n.b. Open stack handlers would be prudent to initialise the default value and button states if this were to be saved for standalone execution.
 
=={{header|Mathematica}}==