Naming buttons in a group from a list in a field

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

Naming buttons in a group from a list in a field

Post by quailcreek »

Hello,
I'm new to the list. Right now I only have a demo version of RR U3 but I am impressed. I've got an app that I've been working on in another scripting package and I'd like to see what it would take to convert to RR. So my question is: How would I, on a simple mouseUp from a button, name the buttons in a group from a list in a field? And, is the order of the buttons controlled by the layer their on? I would be grateful for the assistance.

Regards
Tom
malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Contact:

Post by malte »

Hi Tom,

there are many ways to reference controls in revolution. Using the layer is one option.

in your button:

Code: Select all

on mouseUp
  repeat with i=1 to the number of lines of field "myListField"
    if there is a button i of group "myGroup" then
      set the name of button i of group "myGroup" to line i of field "myListField
    end if
  end repeat
end mouseUp
Please feel free to ask if there are more questions.

All the best,

Malte
quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

Post by quailcreek »

Thanks Malte,
The script worked great. Now the next step. The buttons were named in the order of the layer they were on. Unfortunatly that's not the order I need them to be it. I want the order to be from top left to bottom right. There are three coulmns of 10 buttons in the group. Do the layers of the buttons need to be set or is there a way to control the order otherwise? I'm assuming that setting the layer order will involve a very similar repeat script. But how?

Thansk again
Tom
Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark »

Hi Tom,

You probably need something like this:

Code: Select all

on setLayers
  repeat with x = 1 to number of buttons
    if there is a btn x of grp "MyGroup" then
      put x & comma & (item 1 of the loc of btn x of grp "MyGroup") & "." & ¬ 
      (item 2 of the loc of btn x of grp "MyGroup") & cr after myList
    end if
  end repeat
  sort lines of myList numeric by item 2 of each
  put 0 into x
  repeat for each line myBtn in myList
    add 1 to x
    set the layer of btn (item 1 of myBtn) of grp "MyGroup" to x
  end repeat
end setLayers
To use this script, your 30 buttons have to be placed exactly in the right location, i.e. the x-values of the locations of all buttons on a row have to be equal and the y-values of the locations of all buttons in a columns have to be equal too.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

Post by quailcreek »

Thanks a lot Mark. I see the logic. The script works pretty well exept it goes from top right to bottom left. I need TL to BR. What do I need to change in the script?

Thanks Again
Tom
Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark »

Hi Tom,

It took a little effort, but it was a fun puzzle. Here is a scipt that sets layer numbers, starting at topleft and finishing at bottomright, either horizontally or vertically.

Code: Select all

on setLayers theGroup,theHVFlag
  if theGroup is not a number then put the id of grp theGroup into theGroup
  lock screen
  if char 1 of theHVFlag is "h" then put "21" into myHVFlag else put "12" into myHVFlag
  start editing grp id theGroup
  repeat with x = 1 to number of buttons
    put the id of btn x & comma & (item (char 1 of myHVFlag) of the loc of btn x)*1 & "." & ¬
    format("%03d",(item (char 2 of myHVFlag) of the loc of btn x)) & cr after myList
  end repeat
  sort lines of myList numeric by item 2 of each
  put 0 into x
  repeat for each line myBtn in myList
    add 1 to x
    set the layer of btn id (item 1 of myBtn) to x
    set the label of btn id (item 1 of myBtn) to x
  end repeat
  set the editbackground to false
  unlock screen
end setLayers
usage:

setLayers <group name|group ID>,<"h"|"v">

examples:
setLayers "My Group","h"
setLayers 1010,"v"


Enjoy,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

Post by quailcreek »

Mark,
Awesome! Flawless! This is something I could not do in the other scripting software. If you like puzzles, I can keep you busy for quit awhile.

Regards
Tom
Post Reply