Change Button Label

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
HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

Change Button Label

Post by HJay » Wed Mar 21, 2012 8:23 pm

Hi

Is it possible to change a button lable when the button is clicked on? so if it was called 1 and when it was clicked on is was then changed to another random number.

I tried to search the forum but it wouldn't let me as the words where to common?

Thanks in advance :)

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

Re: Change Button Label

Post by Klaus » Wed Mar 21, 2012 8:28 pm

Hi HJay,

you are almost there :D

Code: Select all

on mouseup
  set the label of ME to "New label text here..."
  ## or to whatever to you like.
ernd if
Hint:
Please do not NAME buttons with numbers!
Name them "button1" if necessary, but NOT 1 or 2 etc...
But you can set the LABEL to whatever you like!

Ah, and welcome to the forum :D

Did you see these great learning stacks over here?
http://www.runrev.com/developers/lesson ... nferences/


Best

Klaus

HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

Re: Change Button Label

Post by HJay » Wed Mar 21, 2012 10:24 pm

Hi Klaus

Thank you for your speedy responce. I am a complete beginner as you may have already guessed :oops: I am using the free trial at the moment. I have repeated watched and tried to replicate the tutorials for the task list app thingy from the nice scottish lady. I have printed out and poured over the user guide. I have searched youtube for other video tutorials. I have made the 3 5 7 game thingy. I have studied user samples which have overwhelmed me somewhat. I have learned a bit and made my head hurt a lot :D I have been unable to find a tutorial along the line of what I am trying to do though. So with 12 days left of my trial in which to find out if LiveCode will be able to do what I need I have started asking questions. Hopefully my questions will not be too dull :) I hadn't found the link to the learning stacks so that was useful thank you.

I managed to get your code to work :D I didn't know you could use ME so have learned something else new.

What I am actually trying to do is make a stack that will display a number and two buttons below, ButtonL and ButtonR. I want both buttons to display numbers, one that matches the sample number above. When a pupil clicks on the hopefully correct button I want another matching number problem to be be displayed.

What I think I should do is make the buttons variables and get the data from a list on another card. I think I will have to think a bit more about chunks at this point. Is this the direction to go in or am I stumbling down the wrong ally.

Any help very much appreaciated

HJay

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Change Button Label

Post by mwieder » Wed Mar 21, 2012 10:36 pm

Setting the *name* of a button and setting the *label* of a button are different things. The name is how you will refer to the button in your script. You can easily display numbers in the buttons by setting their labels. But it's a bad idea to set the name itself to a number. Name the button once when you're designing the screen and then don't change it after that.

The label is what gets displayed as the button's text on the screen:

Code: Select all

set the label of button "ButtonL" to 42

Code: Select all

put pi * 3 into tVariable
set the label of button "ButtonR" to tVariable
Welcome to LiveCode and keep asking questions. :)

HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

Re: Change Button Label

Post by HJay » Thu Mar 22, 2012 9:14 am

Thanks for that I am moving forward :D

Just for the record I would just like to point out that I have never committed the heinous crime of giving a button a name that was a number. This was purely by luck though and I now solemnly swear never in future to name a button by a number. :lol:

I have a card with 2 buttons on in named ‘ButtonL’ and ‘ButtonR’

I have this code on the card (because at a later date I want both buttons to change when I click on either of them, is this the right thing to do?):-

Code: Select all

on mouseUp
    put any item of "0,1,2,3,4,5,6,7,8,9" into  tVariable
    set the label of button "ButtonR" to tVariable
end mouseUp
and when I click ‘ButtonR’ sure enough I get a random single digit number :)

...but when I click on ’ButtonL’ which I have labelled ‘Left’ it changes the label on ‘ButtonR’ :(

which is confusing me as as far I can see I haven’t told ‘ButtonL’ to do anything other than label itself ‘Left’. Why is this happening?

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Change Button Label

Post by mwieder » Thu Mar 22, 2012 5:47 pm

You'll want to have a look at Richard Gaskin's article on the message path.

http://www.fourthworld.com/embassy/arti ... _path.html

But basically, your mouseUp handler in the card is being triggered by the "ButtonL" button probably because there is no mouseUp handler in that button. So lacking a handler there, the card is the next place to check. And bingo! there's a mouseUp handler to execute. If you change your code in the stack script to read

Code: Select all

    on mouseUp
        put any item of "0,1,2,3,4,5,6,7,8,9" into  tVariable
        set the label of button "ButtonR" to tVariable
        put any item of "0,1,2,3,4,5,6,7,8,9" into  tVariable
        set the label of button "ButtonL" to tVariable
    end mouseUp
you should have what you want.

HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

Re: Change Button Label

Post by HJay » Thu Mar 22, 2012 6:37 pm

Thank you and thank you for the link.

I had come across the message path in the live code tutorials, which is why I thought that I should put the code on the card rather than the buttons?
I still don’t see why code that only tells ButtonR to do something should change ButtonL though. I did a test putting some code onto ButtonL and it stoped changing when ButtonR was pressed, as you said it would. Does this mean that every button I use has to have code associated with it otherwise it may do something I have not asked it to do?

I am now off to learn some more about chunks and arrays. Thank you so much for pointing me in the right direction. :D

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

Re: Change Button Label

Post by Klaus » Thu Mar 22, 2012 6:47 pm

Hi HJay,

yes, if you have 10 buttons on your card and none of them does have a "mouseup" script
but you have a "mouseup" in the script of the CARD, then ALL buttons will execute
THAT "mouseup" script of the card.

This is what the "message hierarchie" is all about :D

In your case (only two buttons) you could still use the mouseup in the card, but then CHECK
which of your two buttons has been clicked and act accordingly:

Code: Select all

on mouseUp
    put any item of "0,1,2,3,4,5,6,7,8,9" into  tVariable

   # no we use "THE TARGET" to see what button has been clicked
   put the short name of the target into tClickedButtonName

  ##  ButtonR has been cliked, so change the label of the other button:
   if tClickedButtonName = "ButtonR" then
          set the label of button "ButtonL" to tVariable
   else
    ## and vice versa :-)
      set the label of button "ButtonR" to tVariable
   end if
end mouseUp
Best

Klaus

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Change Button Label

Post by mwieder » Thu Mar 22, 2012 7:09 pm

This is what the "message hierarchie" is all about :D
Yes, and this is part of what makes LiveCode so powerful.
Look - it really takes about six weeks for all this to sink in, so don't worry about trying to make sense of everything all at once.
You're off to a great start so far.

HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

Re: Change Button Label

Post by HJay » Tue Mar 27, 2012 5:55 pm

Thank you both for your helpful replies, I do feel like I'm swimming through treacle at the moment but will keep going :)

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

Re: Change Button Label

Post by Klaus » Tue Mar 27, 2012 5:57 pm

HJay wrote:Thank you both for your helpful replies, I do feel like I'm swimming through treacle at the moment but will keep going :)
Yeah, bravo, don't give up, you won't regret!

And thank you for teaching me a new english word: treacle :D


Best

Klaus

HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

Re: Change Button Label

Post by HJay » Mon Apr 02, 2012 10:08 am

lol is there no treacle in Germany? Or is it just called something else?

I am putting together some flashcards to aid my learning.

Would you mind adding a few Q. and A.'s you think should be learned at the beginning? I need to get about sixty before I can start doing them.

http://forums.runrev.com/viewtopic.php? ... 392#p54392


Thanks in advance

HJay

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

Re: Change Button Label

Post by Klaus » Mon Apr 02, 2012 12:07 pm

Hi HJay,
HJay wrote:lol is there no treacle in Germany? Or is it just called something else?
???

Of course we have "Rübenkraut" here in germany, but I did not know the english word for it
until your post forced me to look it up in a dictionary! 8)

OK, for your Q&A:
The german word for "treacle" is ______________ :D


Best

Klaus

HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

Re: Change Button Label

Post by HJay » Mon Apr 02, 2012 1:43 pm

:lol: I thought kraut meant cabbage, well you learn something new everday :D

HJay

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

Re: Change Button Label

Post by Klaus » Mon Apr 02, 2012 2:28 pm

Hi HJay,

well "cabbage" is actually "Kohl" in german, just like the ex-chancellor :-)


Best

Klaus

Post Reply