Program Button Script Programically

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
gpearson
Posts: 84
Joined: Wed Feb 03, 2010 12:55 pm

Program Button Script Programically

Post by gpearson » Mon Mar 17, 2014 2:33 pm

Before I posted this, I have been searching the forums and google to find examples of how to program a button on a card within the openCard script of the card instead of adding the script to the button as this specific button would need to change the script depending on the user's interaction with the card.

The dictionary of LC editor's example was not very good as it only showed how to empty the button's script. I would like the script to be if the environment is development, then replace the script button with something totally different then if the application was ran on a standalone and the button is to quit the application.

If someone has a resource already online, let me know where an example is located at so I can continue my Livecode Learning Process.
---
Graham Pearson
Goshen, IN USA

We Are Closed Today is your single internet resource for Indiana Area School Closings, Visit http://www.weareclosedtoday.com

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Program Button Script Programically

Post by FourthWorld » Mon Mar 17, 2014 3:03 pm

Can you describe how much the code might vary in response to the user interaction? There are many ways this could be done, including changing the behavior object assigned to the button, and probably wouldn't require actually changing the button's script. But to know which method would be best in your circumstance it'll be helpful to know what those changes would entail.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Program Button Script Programically

Post by Klaus » Mon Mar 17, 2014 3:07 pm

Hi Graham,

there are limits on creating scripts "on the fly"!
But we have "behaviors"! :D

I would recommend to script a couple of behaviors and
set the behavior of your special button to one of these
depending on your needs.

Example, presuming the behavior buttons are on teh same card:

Code: Select all

on opencard
  if this_or_that then
      set the behavior of btn "your special button here..." to the long ID of btn "behavior1"
   else
     set the behavior of btn "your special button here..." to the long ID of btn "behavior2"
  end if
end opencard
You get the picture :D


Best

Klaus

gpearson
Posts: 84
Joined: Wed Feb 03, 2010 12:55 pm

Re: Program Button Script Programically

Post by gpearson » Mon Mar 17, 2014 3:50 pm

In the first application I am working on in Livecode as a Port of Adobe Flex/Air, I have a custom card that will display error messages within the application to make them "Pretty" instead of the answer box. This card called Alerts has 3 buttons at the bottom. The righthand is called Cancel_Button and if "the patform" does not equal the platforms which the application has been written for, this Cancel Button has been changed to Quit.

By default the onMouseUp of the Cancel Button will send the user to the Main Card of the stack. But in this case, it would need to detect if the environment is development or standalone. If it is development then do nothing. But if it is standalone then quit the application.
FourthWorld wrote:Can you describe how much the code might vary in response to the user interaction? There are many ways this could be done, including changing the behavior object assigned to the button, and probably wouldn't require actually changing the button's script. But to know which method would be best in your circumstance it'll be helpful to know what those changes would entail.
---
Graham Pearson
Goshen, IN USA

We Are Closed Today is your single internet resource for Indiana Area School Closings, Visit http://www.weareclosedtoday.com

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: Program Button Script Programically

Post by dave.kilroy » Mon Mar 17, 2014 4:03 pm

Hi Graham

It sounds like you just need a simple if statement making use of the 'environment' function - something like:

Code: Select all

on mouseUp
   if the environment is "development" then
      --do nothing
   else
      --code to quit your app
end mouseUp
gpearson wrote:It would need to detect if the environment is development or standalone. If it is development then do nothing. But if it is standalone then quit the application.
"...this is not the code you are looking for..."

gpearson
Posts: 84
Joined: Wed Feb 03, 2010 12:55 pm

Re: Program Button Script Programically

Post by gpearson » Mon Mar 17, 2014 4:11 pm

I can do that if the button would always be the same but when I get to the section of the SQLite database, this button might need to be used to email developer of an error so its code will change along with the label of the button for this card.

I think right now, I might just create other buttons with the proper code, and position them where I need them to be along with making them visible or not.
---
Graham Pearson
Goshen, IN USA

We Are Closed Today is your single internet resource for Indiana Area School Closings, Visit http://www.weareclosedtoday.com

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: Program Button Script Programically

Post by dave.kilroy » Mon Mar 17, 2014 4:21 pm

Hi Graham

Yep having different buttons and showing/hiding them as required would work - but it might be simpler to just write the code in a single button! (reading results from SQLite queries etc and branching to different actions as a result?)

Kind regards

Dave
"...this is not the code you are looking for..."

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Re: Program Button Script Programically

Post by sefrojones » Tue Mar 18, 2014 2:41 am

would this help?

Code: Select all

if the label of me is "whatever" then

do "whatever" stuff

else

do Some Other stuff

end if
then just set the label of the button in the opencard script?

Post Reply