Copy a button at runtime

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
jacobb20
Posts: 9
Joined: Mon Nov 26, 2012 4:21 pm

Copy a button at runtime

Post by jacobb20 » Mon Nov 26, 2012 4:26 pm

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 :)

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

Re: Copy a button at runtime

Post by Mark » Mon Nov 26, 2012 6:37 pm

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
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

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

Re: Copy a button at runtime

Post by jmburnod » Mon Nov 26, 2012 7:01 pm

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
Attachments
CopyBtnLeg24.livecode.zip
(1.55 KiB) Downloaded 251 times
https://alternatic.ch

jacobb20
Posts: 9
Joined: Mon Nov 26, 2012 4:21 pm

Re: Copy a button at runtime

Post by jacobb20 » Mon Nov 26, 2012 7:44 pm

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 :)

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Copy a button at runtime

Post by Klaus » Mon Nov 26, 2012 7:55 pm

Hi Jacob,

1. welcome to the forum! :D

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

jacobb20
Posts: 9
Joined: Mon Nov 26, 2012 4:21 pm

Re: Copy a button at runtime

Post by jacobb20 » Mon Nov 26, 2012 8:08 pm

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.

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Copy a button at runtime

Post by Klaus » Mon Nov 26, 2012 8:23 pm

Hi Jacob,

aha...

Still not the slightest idea what the buttons are supposed to do 8)

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! :D

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

jacobb20
Posts: 9
Joined: Mon Nov 26, 2012 4:21 pm

Re: Copy a button at runtime

Post by jacobb20 » Mon Nov 26, 2012 9:20 pm

Thanks...target is exactly what I want!

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Copy a button at runtime

Post by jacque » Tue Nov 27, 2012 7:40 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

jacobb20
Posts: 9
Joined: Mon Nov 26, 2012 4:21 pm

Re: Copy a button at runtime

Post by jacobb20 » Tue Nov 27, 2012 10:31 pm

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.

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

Re: Copy a button at runtime

Post by jmburnod » Tue Nov 27, 2012 11:53 pm

Hi Jacob
If you want get the name of the group of an object, use "the owner of the target"
Best
Jean-Marc
https://alternatic.ch

Post Reply