applying commands to multiple objects

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
gfquad
Posts: 7
Joined: Wed Nov 13, 2013 11:29 am

applying commands to multiple objects

Post by gfquad » Wed Nov 13, 2013 12:10 pm

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

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: applying commands to multiple objects

Post by jmburnod » Wed Nov 13, 2013 12:19 pm

Hi gfquad,
Welcome in this forum
Is there a way to apply the same command to multiple objects?
Yes :D

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
Best regards
Jean-Marc
https://alternatic.ch

gfquad
Posts: 7
Joined: Wed Nov 13, 2013 11:29 am

Re: applying commands to multiple objects

Post by gfquad » Wed Nov 13, 2013 12:27 pm

Thanks Jean-Marc,
Does that only work with numbered objects? What if the images were called "history", "setup", "info" etc?
Thanks
Geoff

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: applying commands to multiple objects

Post by Dixie » Wed Nov 13, 2013 12:43 pm

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

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: applying commands to multiple objects

Post by jmburnod » Wed Nov 13, 2013 12:46 pm

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)

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

gfquad
Posts: 7
Joined: Wed Nov 13, 2013 11:29 am

Re: applying commands to multiple objects

Post by gfquad » Wed Nov 13, 2013 2:08 pm

Thanks both, I've used a combination of your methods, it's been really helpful.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: applying commands to multiple objects

Post by dunbarx » Wed Nov 13, 2013 4:24 pm

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

Post Reply