Setting text into a button.

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

CAsba
Posts: 427
Joined: Fri Sep 30, 2022 12:11 pm

Setting text into a button.

Post by CAsba » Wed Oct 05, 2022 12:54 pm

I just put the following code
"...
set the label of btn "1006" to "Raise an invoice"
...
"
into a button, set different colors for text and background, and don't get the text on the button.
'Errors' shows 'No errors'

Any ideas please ?

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

Re: Setting text into a button.

Post by Klaus » Wed Oct 05, 2022 1:00 pm

Sure its name is "1006"? Not "button id 1006"?
It is not recommended to name objects with NUMBERS!
Give your button a name and try again.

Or try this:

Code: Select all

...
set the label of btn id 1006 to "your string here..."
...
We need to address objects with their names or ID:

Code: Select all

...
set the label of btn "my button" to "your string here..."
...
set the label of btn id 1006 to "your string here..."
...

stam
Posts: 3069
Joined: Sun Jun 04, 2006 9:39 pm

Re: Setting text into a button.

Post by stam » Wed Oct 05, 2022 1:10 pm

Yep pretty sure Klaus is on the money there.
1006 suggests an ID - so the button should be referred to as
btn ID 1006 (without any quotes).

CAsba
Posts: 427
Joined: Fri Sep 30, 2022 12:11 pm

Re: Setting text into a button.

Post by CAsba » Wed Oct 05, 2022 1:58 pm

I just entered
"
...
set the label of btn id 1006 to "Raise an invoice"
...
"
without success

stam
Posts: 3069
Joined: Sun Jun 04, 2006 9:39 pm

Re: Setting text into a button.

Post by stam » Wed Oct 05, 2022 2:06 pm

CAsba wrote:
Wed Oct 05, 2022 1:58 pm
set the label of btn id 1006 to "Raise an invoice"
...
without success
The only explanation to that is that the button does not have ID 1006....
Open the property panel for the button - if no name is assigned then the titlebar of the property panel should read button id xxxx (id xxxx), or else button "<button name>" (id xxxx) - make sure the ID you're using is correct.

i just made new button that was given the id 343434.

Code: Select all

set the label of button id 343434 to "test"
sets the label (the text) of the button to 'test'

Alternatively, and this would be my preference, you can name the button (easier to remember) and still display a different label:

Code: Select all

set the label of button "pay" to "Raise and invoice"
The short name of the button is 'pay' the name of the button is button "pay" and the long name of the button is button "pay" of card "<card name>" of stack "<stack name>". All of these are mutable and different from the ID which is immutable. Note however that if you copy/paste the button, the names remain the same but the ID changes to a different immutable number.

HTH
Stam

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

Re: Setting text into a button.

Post by Klaus » Wed Oct 05, 2022 2:31 pm

CAsba wrote:
Wed Oct 05, 2022 1:58 pm
I just entered

Code: Select all

...
set the label of btn id 1006 to "Raise an invoice"
...
without success
Did you script:

Code: Select all

on mouseup
   set the label of btn id 1006 to "Raise an invoice"
end mouseup
You need to click the button then or nothing will happen!
If not "mouseup" where did you enter this?

CAsba
Posts: 427
Joined: Fri Sep 30, 2022 12:11 pm

Re: Setting text into a button.

Post by CAsba » Wed Oct 05, 2022 2:51 pm

I entered it into object > code

stam
Posts: 3069
Joined: Sun Jun 04, 2006 9:39 pm

Re: Setting text into a button.

Post by stam » Wed Oct 05, 2022 2:54 pm

The code fragment as is has no trigger to make it run. Usually this will be a mouse action of some kind when it comes to buttons, but could equally be called in code from elsewhere.

But ultimately the app will need to know a user action that triggers this, eg mouseUp as Klaus showed, or could be for example mouseEnter, mouseLeave and so on.
Or run the command as is in the message box if you just want to test it.

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

Re: Setting text into a button.

Post by Klaus » Wed Oct 05, 2022 3:29 pm

CAsba wrote:
Wed Oct 05, 2022 2:51 pm
I entered it into object > code
WHAT object?

CAsba
Posts: 427
Joined: Fri Sep 30, 2022 12:11 pm

Re: Setting text into a button.

Post by CAsba » Wed Oct 05, 2022 3:53 pm

The button

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

Re: Setting text into a button.

Post by Klaus » Wed Oct 05, 2022 4:07 pm

The button where you want to set the label?
If yes, then you need to actually CLICK that button to execute that script.

stam
Posts: 3069
Joined: Sun Jun 04, 2006 9:39 pm

Re: Setting text into a button.

Post by stam » Wed Oct 05, 2022 4:08 pm

In the button’s script (object > code) you need to put a mouseUp handler so the button knows what to do when clicked.
Inside the mouseUp handler you run the code fragment posted above….
Klaus posted the full example above. It’s a rather basic thing that most of us take for granted now, but I remember forgetting to do this at the start.

Keep in mind the button can react to other events (like a rollover effect for example) so you need to tell the button what event to react to… hope that makes sense…

CAsba
Posts: 427
Joined: Fri Sep 30, 2022 12:11 pm

Re: Setting text into a button.

Post by CAsba » Wed Oct 05, 2022 4:33 pm

Hi,
I selected mouseup handler, entered the code, came out of script and clicked the button.
Nothing happened, no text on button.

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

Re: Setting text into a button.

Post by Klaus » Wed Oct 05, 2022 4:41 pm

Is the button with the script the same button you want to set the label for?
Make sure the ID is correct, check the inspector.
However I highly recommend to give your button(s) meaningfull names!

Give the button you want to set the label for a NAME and try again.

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

Re: Setting text into a button.

Post by dunbarx » Wed Oct 05, 2022 5:29 pm

CAsba.

Before steam starts coming out of the team here, open the attached stack. Clicking on the "Set Label" button will set the label of the lower left button to a random animal. You can see the handler in the "Set Label" button.

So it works, you see. The point is that somehow, somewhere in your own stack you are confusing LiveCode by using numbers as object references. DO NOT EVER DO THAT. LC has enough trouble keeping up with the several numbers it already assigns to each control without any of us adding to that burden.

If you do want to use numbers, and there are often very good reasons to do so, you have to couch them a little differently. For example, name your buttons "B1", B2" "B1006". That sort of thing, and then LC will not care, nor become confused.
SetLabel.livecode.zip
(866 Bytes) Downloaded 136 times
Craig

Post Reply