Home
Project Folders
Teacher Resources
Student Resources
How Do I?
Literacy
MWForum
Site Map
Search
Our Team
How do I change the contents of a textbox with the click of a button?
There are two ways to change the contents of a textbox with commands. (Let's assume that the textbox is named text1 and the words "This is the new text" stand for the new contents of the textbox.)
  1. ct insert [This is the new text.]
  2. settext1 [This is the new text.]
Either method has the same effect: replacing the contents of the textbox with the words inside the brackets. When the command(s) are placed in a procedure, they can be activated by a button.

One button can also be used to call a different procedure with each click, so that a whole story can unfold on one page. In this demo, each procedure triggers a small animation along with the new text.

Click next to trigger the next animation. Click start_over if you want to reset the story.
to startup
make "newtext 1
startstory
end

to startstory
setsh "kitty1
setpos [-25 -19]
st
settext1 [Once upon a time a cat named Ralph visited the MicroWorlds in Action website.]
end

to next
run word "continuestory :newtext
make "newtext :newtext + 1
end

to continuestory1
settext1 [Ralph is showing us how he can dance while we read about it.]
repeat 8 [setsh "kitty2 wait 3 setsh "kitty1 wait 3]
end

to continuestory2
settext1 [Now Ralph is showing us how he can jump up and down while we read about it.]
repeat 10 [setsh "kitty3 sety ycor + 20 wait 2 setsh "kitty1 sety ycor - 20 wait 2]
end

to continuestory3
settext1 [Now Ralph is showing us how he can dance all over the page.]
repeat 25 [setsh pick [kitty1 kitty2 kitty3] seth random 360 fd random 100 wait 2]
setpos [-25 -19]
setsh "kitty1
end

to continuestory4
settext1 [Now Ralph is silently clapping his hands.]
repeat 10 [setsh "kitty1 wait 3 setsh "kitty4 wait 3]
end

to continuestory5
settext1 [And now Ralph is doing his leaping jumping jacks.]
repeat 10 [setsh "kitty3 sety ycor + 30 wait 3 setsh "kitty5 sety ycor - 30 wait 4]
setsh "kitty1
end

to continuestory6
settext1 [Ralph can even plant trees all over the meadow!]
repeat 20 [setsh "kitty1 sety -125 + random 180 setx -300 + random 600 wait 2 setsh "tree stamp wait 2]
setsh "kitty1
setpos [-59 -20]
end

to continuestory7
settext1 [Don't worry... Ralph can clap his hands 3 times and make the trees disappear.]
wait 10
repeat 3 [setsh "kitty4 wait 3 setsh "kitty1 wait 3]
clean
end

to continuestory8
settext1 [Now Ralph shows us how he enjoys dancing as he leaves us.]
seth 90
repeat 10 [setsh "kitty3 fd 8 wait 2 setsh "kitty5 fd 8 wait 2]
ht
end

to continuestory9
settext1 [The End]
make "newtext -1
end

to continuestory0
settext1 [You haven't had enough??? Okay, let's start over....]
wait 25
start_over
end

to start_over
startup
end

Notes:  

Each procedure for changing the text uses the same base name but ends with a different number. The procedures are numbered in order.

The next procedure uses word to combine the base name with the value of the variable newtext. It runs the procedure with this name and then increases newtext by 1, so when the next button is clicked again, it will call the procedure ending with the next number. The last procedure in the story sets newtext to -1, and the newtext procedure immediately increases this number by 1, so that the next click of the button calls the procedure ending with 0.

By the way, Ralph can erase the trees without also cleaning the landscape because freezebg was used to freeze the background right after it was drawn.

« Previous
Menu


© copyright 2001 - 2008  OpenWorld Learning.   All rights reserved.