Page 1 of 1
Copy a button at runtime
Posted: Mon Nov 26, 2012 4:26 pm
by jacobb20
I would like to create several rows of buttons at runtime, dynamic from a database. I get an error right below the copy button comment. I've tried the copy and clone commands as well. How do I do this? I have a button called "btnCopy" on the card and would like to copy it existing settings (skin, text format) and just change the location.
This code isn't set to work yet with the positioning, just trying to get it to copy the button.
put empty into field "results"
put 1 into xLoop
repeat until revQueryIsAtEnd(dtEndUse)
local cEndUse, btnEndUse
put revDatabaseColumnNamed(dtEndUse, "end_use") into cEndUse
put cEndUse & return after field "results"
# copy the btnCopy button
put button "btnCopy" into btnEndUse
set the location of btnEndUse to 50, 50+y
set the name of btnEndUse to "btn1_" & xLoop
set the label of btnEndUse to cEndUse
revMoveToNextRecord dtEndUse
put xLoop + 1 into xLoop
put y + 50 into y
end repeat
Thanks!
EDIT: I am a complete beginner, first project in livecode

Re: Copy a button at runtime
Posted: Mon Nov 26, 2012 6:37 pm
by Mark
Hi,
The put command is only for data (from a variable, control, property or file). Your script only puts the text of button "btnCopy" into variable btnEndUse. Use the clone command to duplicate a control or use the copy command with a destination. Search for "clone" or "copy" in the LiveCode dictionary.
Actually, I don't think that the line
Code: Select all
put button "btnCopy" into btnEndUse
would cause an error. Are you sure that it is this line that throws the error?
Jacob might be a Dutch name. If you're Dutch, check out the Dutch forum at
http://runrev.info/rrforum
Kind regards,
Mark
Re: Copy a button at runtime
Posted: Mon Nov 26, 2012 7:01 pm
by jmburnod
Hi Jacob20,
As Mark said i think that is not
Code: Select all
put button "btnCopy" into btnEndUse
but
Code: Select all
set the location of btnEndUse to 50, 50+y
cause an error
The stack in attachment try to do what you want
Best regards
Jean-Marc
Re: Copy a button at runtime
Posted: Mon Nov 26, 2012 7:44 pm
by jacobb20
Thanks jmburnod! Thats perfect.
I am not dutch Mark, from USA.
Now to figure out how to handle the events on these dynamic buttons! Any suggestions would be greatly appreciated

Re: Copy a button at runtime
Posted: Mon Nov 26, 2012 7:55 pm
by Klaus
Hi Jacob,
1. welcome to the forum!
2. please check these stacks, great learning resources (and one needs to know the basics!)
http://www.runrev.com/developers/lesson ... nferences/
Now to figure out how to handle the events on these dynamic buttons!
What are they supposed to do?
Best
Klaus
Re: Copy a button at runtime
Posted: Mon Nov 26, 2012 8:08 pm
by jacobb20
So there are three levels of End Use. There are roughly 15 level 1 options...below is just an example. The user would select one level 1 option, which would then move onto level 2.
Level 1:
Baked Goods
Hardware
...etc
Level 2: (assume the user selected Baked Goods, now I need to delete those level 1 buttons and show the level 2 options in the Baked Goods category from the database)
Cookies
Muffins
...etc
Level 3: (user selected cookies)
Chocolate chip
Vanilla
...etc
But they all need to be dynamic. I assume I put an mouseUp event on the Card script, check if whatever is clicked is a button (trying to figure this out), and use the button name that was clicked to execute the sql for the next level.
Re: Copy a button at runtime
Posted: Mon Nov 26, 2012 8:23 pm
by Klaus
Hi Jacob,
aha...
Still not the slightest idea what the buttons are supposed to do
But this:
jacobb20 wrote:I assume I put an mouseUp event on the Card script, check if whatever is clicked is a button (trying to figure this out),...
seems to be a great idea!
You can use "the target" in the "Mouseup" script in the card script to check what exactly has been clicked, like this (in the card script!):
Code: Select all
on mouseup
## 1. check if a BUTTON has been clicked, if not, then eit the hanlder!
## I prefer to check if a conditon is NOT true first, this way you can avoid long nested "if... then" constructions!
if word 1 of the target <> "button" then
exit mouseup
end if
## 2. check the NAME of the BUTTON that has been clicked,
## if the name does not start with "end_use_" (or whatever you want) then exit handler
put the short name of the target into tName
if NOT tName begins with "end_use_" then
exit mouseup
end if
## Now you can even check the number at the end of your button
set itemdelimiter to "_"
## check ITEMDELIMITER in hte dictionary!
put item -1 of tName into tNumber
## ...item -1 of... = the last item of...
## OK, now you have the name of the button clicked in tName and the number of the clicked button in tNumber
## do whatever you need to with this :-)
end mouseup
Best
Klaus
Re: Copy a button at runtime
Posted: Mon Nov 26, 2012 9:20 pm
by jacobb20
Thanks...target is exactly what I want!
Re: Copy a button at runtime
Posted: Tue Nov 27, 2012 7:40 pm
by jacque
If there will always be the same number of buttons in each set, I wouldn't create and delete them. I'd put them in groups and then hide or show the correct group as needed. You can rename existing buttons, or even better, set their labels dynamically. By setting a label, the button name remains the same so that you can address it in a script, but the visual label the user sees on screen can be anything.
Re: Copy a button at runtime
Posted: Tue Nov 27, 2012 10:31 pm
by jacobb20
well, the number of buttons can change if they add/delete a category, category names can change.
Question though with the groups. Skinned buttons are not quite getting me where I need to go, so I've been playing around with groups (a graphic, and a few labels). With buttons I can't seem to get text to wrap, center it on the skin, etc. But I can't quite figure out how to tell if the user clicked a group instead of a button..."target" returns what part of the group the user clicked on, like the label field or graphic.
Re: Copy a button at runtime
Posted: Tue Nov 27, 2012 11:53 pm
by jmburnod
Hi Jacob
If you want get the name of the group of an object, use "the owner of the target"
Best
Jean-Marc