common buttons with different labels

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
Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

common buttons with different labels

Post by Newbie4 » Wed May 23, 2012 7:38 pm

I have buttons numbered (labelled 1-9 and a-z) and on mouseDown, I want to use the label/value of that key. Is there an easier way to do it than writing a handler for each one?

I tried giving each button the same name and putting one handler for that name on the card script but that did not work.

Any suggestions?
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: common buttons with different labels

Post by dunbarx » Wed May 23, 2012 7:43 pm

Hi.

Don't use numbers for names. Just don't. This is not prohibited, but, well, if you do, you will soon get flummoxed. Go back and name your buttons "a1", "a2" or something like that if your scheme warrants that sort of thing.

Put something like this in your card script:

Code: Select all

on mouseup
   if button is in the target then answer the target
end mouseup
Make sure there is no "mouseUp" handler in any of your buttons, or if there is, that the last line reads: "pass mouseUp".

This gets into fundamental knowledge of the message and hierarchy workings of LiveCode. You MUST practice this sort of thing, Why does this simple handler in the card script work at all? What would happen if the central code line read simply: "answer the target". How could you work this so that buttons on more than one card were also dealt with?

Write back...

Craig Newman


Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

Re: common buttons with different labels

Post by Newbie4 » Wed May 23, 2012 8:33 pm

dunbarx wrote:Hi.

Don't use numbers for names. Just don't. This is not prohibited, but, well, if you do, you will soon get flummoxed. Go back and name your buttons "a1", "a2" or something like that if your scheme warrants that sort of thing.

Put something like this in your card script:

Code: Select all

on mouseup
   if button is in the target then answer the target
end mouseup
Make sure there is no "mouseUp" handler in any of your buttons, or if there is, that the last line reads: "pass mouseUp".

This gets into fundamental knowledge of the message and hierarchy workings of LiveCode. You MUST practice this sort of thing, Why does this simple handler in the card script work at all? What would happen if the central code line read simply: "answer the target". How could you work this so that buttons on more than one card were also dealt with?

Write back...

Craig Newman
I tried that and got the message:
compilation error (Expression: double binary operator) near "is", char 7)
I don't understand?
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Re: common buttons with different labels

Post by BvG » Wed May 23, 2012 9:03 pm

that's probably because he made a small error in that example, just to test if you've actually learned, or simply copy pasted his code without thinking :P
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

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

Re: common buttons with different labels

Post by Klaus » Wed May 23, 2012 9:03 pm

Hi nbewbie4,

no idea what the engine is complaining about but try this:

Code: Select all

on mouseup
   if word 1 of the target = "button" then 
     answer the label of target
   end if
end mouseup
Please take Craigs advice and use the LABEL (what you see on the screen <> the NAME of a button!)
for your "numbering" conventions, with the above script you should be able to do whatever you want to do :D


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: common buttons with different labels

Post by dunbarx » Wed May 23, 2012 9:09 pm

Bjoernke was being kind. If I actually tried the script, instead of just winging it, I might have put "button" in quotes, like a good boy.

And newbie, the reason not to use numbers as names is that these frequently conflict. You will, many times, want to do something with "button 3". The engine will not be able to distinguish between the third button, and a button named "3". That is the reason NOT to do this, ever. Trust us.

But the label, as opposed to the name, is not subject to this confusion, because LC does not allow one to order a control by label. It is just that, text that is displayed, and though it is a property like any other, it will not conflict in the same way. I generally use names alone; rarely do I need a label as well, and "naming" controls properly causes no problems. But labels have their uses, and you generally find them to be helpful when the situation calls for an extra layer of identification.

You may have skirted this, obliquely, when you tried using the same name for many buttons, trying to corral them with a single handler. This was clever, but presumptuous. You HAVE to get this message/hierarchy knowledge firmly implanted. it is the rock bottom foundation for developing as a programmer in this environment, is easy to understand, and contains enormous power.

You have not answered my three questions...

Craig

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

Re: common buttons with different labels

Post by Newbie4 » Thu May 24, 2012 1:24 am

This worked:
I named the buttons - "b1", "b1", "ba", "bb", etc and used the following script on the card

on mouseUp
 local tKey
put the short name of the target into tKey   
put the second char of tKey into field "Field1"
end mouseUp

There must be a less awkward way of doing this, I will keep trying.

Sorry about your questions, I am still working on them. (e.g. "target" is a new term to me. I have to read up on it and how to use it)

But thanks for your help
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

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

Re: common buttons with different labels

Post by Klaus » Thu May 24, 2012 9:32 am

Hi Newbie4,
Newbie4 wrote:This worked:
I named the buttons - "b1", "b1", "ba", "bb", etc and used the following script on the card

Code: Select all

on mouseUp
    local tKey
   put the short name of the target into tKey   
   put the second char of tKey into field "Field1"
end mouseUp
Good, but this script will also execute when you click on the "naked" card (not on a button!)
so you might want to add my "if...then..." clause.
Newbie4 wrote:There must be a less awkward way of doing this,...
Well, the script is pretty straighforward and not awkward at all (in LiveCode terms) :D
Newbie4 wrote:(e.g. "target" is a new term to me. I have to read up on it and how to use it)
Check these stacks, if not already done:
http://www.runrev.com/developers/lesson ... nferences/


Best

Klaus

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

Re: common buttons with different labels

Post by Newbie4 » Thu May 24, 2012 3:11 pm

Klaus wrote:Hi Newbie4,
Newbie4 wrote:This worked:
I named the buttons - "b1", "b1", "ba", "bb", etc and used the following script on the card

Code: Select all

on mouseUp
    local tKey
   put the short name of the target into tKey   
   put the second char of tKey into field "Field1"
end mouseUp
Good, but this script will also execute when you click on the "naked" card (not on a button!)
so you might want to add my "if...then..." clause.
What would I check for? Individual buttons? any buttons? not buttons? card?
Newbie4 wrote:There must be a less awkward way of doing this,...
Well, the script is pretty straighforward and not awkward at all (in LiveCode terms) :D

It would be nice to use the label (the number on the button) instead of picking the off the number from the name. That is what I was thinking of
Newbie4 wrote:(e.g. "target" is a new term to me. I have to read up on it and how to use it)
Check these stacks, if not already done:
http://www.runrev.com/developers/lesson ... nferences/

Thank you

Best

Klaus
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

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

Re: common buttons with different labels

Post by Klaus » Thu May 24, 2012 3:23 pm

Hi Newbie4,
Newbie4 wrote:What would I check for? Individual buttons? any buttons? not buttons? card?
Please check my first post!
You want to check if the user really has clicked on a BUTTON and nothing else! No?
I had the feeling that you only want to execute your script when the user clicks a BUTTON...
Newbie4 wrote:It would be nice to use the label (the number on the button) instead of picking the off the number from the name. That is what I was thinking of
You CAN!
You can put whatever you want into the buttons LABEL, but not into the NAME!
LABEL <> NAME!

These two are different:
LABEL is what you and the user see on screen, NAME is what you address the object with in your script!

So NAME your buttons as you already did but add somehting else as the LABEL of your buttons!
Then you can query the LABEL and do whatever you want woith it!

Code: Select all

on mouseup
   if word 1 of the target = "button" then 
     put the label of the target into fld "whatever"
     ## or whatever...
   end if
end mouseup
Best

Klaus

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

Re: common buttons with different labels

Post by Newbie4 » Thu May 24, 2012 5:53 pm

Thanks, your posts have really helped me to understand it all.

I was trying to do that but I did not know how to reference the label of the button that was pressed

e.g.
in the card script:

on mouseUp
put the label of the button ???? into the field "Field1"
end mouseUp

I did not know how to tell which button was pressed. The keyword "target" solves that

I understand now.
Thanks again.
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

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

Re: common buttons with different labels

Post by Klaus » Thu May 24, 2012 5:58 pm

Hio Newbie4,

glad I could help.

And now you also understand my "if... then..." clause, I hope? 8)


Best

Klaus

Post Reply