Button trick?

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
bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Button trick?

Post by bjb007 » Fri Aug 15, 2008 7:59 am

I want to have a button which I can set
to "Auto On" and "Auto Off" to run a script
automatically if the user wishes.

So need a way to get out of a repeat loop
when the label of the button is "Auto On"
and is clicked.

Have so far this - which of course doesn't
exit the loop.

Code: Select all

on mouseUp
  if the label of me is "Auto Off" then
   set the label of me to "Auto On"
  end if
  
  if the label of me is "Auto On" then
  repeat while label of me is "Auto On"
    send "mouseUp" to button btnNextNumber
    wait 1 sec
  end repeat
  end if  
end mouseUp
This, of course, goes on for ever which
isn't what I want.

Expect it's something to do with messages
which I know nothing about - yet.
Life is just a bowl of cherries.

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Fri Aug 15, 2008 9:04 am

try

Code: Select all

wait 1 sec with messages
in the loop, otherwise the wait command blocks any messages to be able to handle the mouse click toggling off the 'Auto On' label (which isn't included in your sample script, but I'm assuming you've already got that sorted).

asayd
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 107
Joined: Mon Apr 10, 2006 7:02 pm
Contact:

Button trick?

Post by asayd » Fri Aug 15, 2008 7:23 pm

Hi bjb007,

It sounds like what you want to do, if I'm following you, is to:

- Exectue some handler repeatedly while your button is in a certain state; and

- Get out of that state and disable execution when the user clicks the button again.

Rather than imbed the send command in a repeat loop it's usually better to use the send in time construct, then have the called handler call itself unless your state has ended. It would go something like this:

Code: Select all

on mouseUp
  if the label of me is "Auto Off" then
    set the label of me to "Auto On"
    send "mouseUp" to button btnNextNumber
  else
    set the label of me to "Auto Off"
  end if
end mouseUp
Then in the called button:

Code: Select all

global gMsgID  -- could be 'local' if you never call it from other scripts
on mouseUp
  -- do stuff here
  if the label of btn "theOtherBtn" is "Auto On" then
    send "mouseUp" to me in 1 second
    put the result into gMsgID"
  else
    cancel gMsgID -- cancels any pending messages
  end if
end mouseUp
For a discussion of the send in time construct, see my web site http://revolution.byu.edu .
Click on Revolution Tutorials by Topic, then Scroll down to "Working with Dates, Time and Timing" and click on the link for "Executing Time-delayed or Repeating Events".

HTH

Devin Asay
Devin Asay
Learn to code with LiveCode University
https://livecode.com/store/education/

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Button

Post by bjb007 » Sat Aug 16, 2008 3:49 am

Thanks SparkOut and asayd.

asayd...
What a great resource your site is.

Must remember to look there in future
when I need some help.
Life is just a bowl of cherries.

Post Reply