applying commands to multiple objects
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
applying commands to multiple objects
Hi, I'm new to Livecode and I'm trying to streamline some of my code. Is there a way to apply the same command to multiple objects? e.g. I want to set the height of several images to a variable which is defined earlier in the code. Currently my code is like this, where tNW is a variable I've previously set:
set the height of image "pic1" to tNW
set the height of image "pic2" to tNW
set the height of image "pic3" to tNW
set the height of image "pic4" to tNW
Surely there is way to write the code in one line to set the height of all the pics to tNW? I can't seem to find a way to do it of if it's even important?
Thanks
set the height of image "pic1" to tNW
set the height of image "pic2" to tNW
set the height of image "pic3" to tNW
set the height of image "pic4" to tNW
Surely there is way to write the code in one line to set the height of all the pics to tNW? I can't seem to find a way to do it of if it's even important?
Thanks
Re: applying commands to multiple objects
Hi gfquad,
Welcome in this forum
Best regards
Jean-Marc
Welcome in this forum
YesIs there a way to apply the same command to multiple objects?

Code: Select all
on setHeightMyControls pHeight
repeat with i = 1 to 4
set the height of image ("pic" & i) to pHeight
end repeat
end setHeightMyControls
Jean-Marc
https://alternatic.ch
Re: applying commands to multiple objects
Thanks Jean-Marc,
Does that only work with numbered objects? What if the images were called "history", "setup", "info" etc?
Thanks
Geoff
Does that only work with numbered objects? What if the images were called "history", "setup", "info" etc?
Thanks
Geoff
Re: applying commands to multiple objects
Code: Select all
on mouseDown
put "histoty,setup.info" into theList
repeat with count = 1 to the number of items of theList
set the height of image (item count of theList) to pHeight
end repeat
end mouseDown
Re: applying commands to multiple objects
LiveCode is very clever
You can use a list of controls as parameter.
I use "control" instead "image" to use the script for all control (fld, btn, image, graphic)
You can use a list of controls as parameter.
I use "control" instead "image" to use the script for all control (fld, btn, image, graphic)
Code: Select all
on setHeightMyControlsByName pHeight,pListName -- pListname = a list of controls one per line.
repeat with i = 1 to the num of lines of pListName
set the height of control (line i of pListName) to pHeight
end repeat
end setHeightMyControlsByName
or faster
on setHeightMyControlsByName pHeight,pListName
repeat for each line tControl in pListName
set the height of control tControl to pHeight
end repeat
end setHeightMyControlsByName
https://alternatic.ch
Re: applying commands to multiple objects
Thanks both, I've used a combination of your methods, it's been really helpful.
Re: applying commands to multiple objects
What Dixie and Jean-Marc have shown you is that LC can construct object references by assembling or deconstructing strings in clever ways. These strings are then interpreted by the engine.
It is a thing of beauty. Do practice with these, stepping through the handlers they have offered, and watch how each new iteration through the loop creates just the reference you needed, so that the "set" command has just the information it needs to address each object in turn.
Craig Newman
It is a thing of beauty. Do practice with these, stepping through the handlers they have offered, and watch how each new iteration through the loop creates just the reference you needed, so that the "set" command has just the information it needs to address each object in turn.
Craig Newman