Page 1 of 1
Populating menu with label and value
Posted: Wed Feb 17, 2016 11:04 pm
by electricclay
A typical HTML menu has a label and a corresponding value. I'm new to LiveCode but all the examples I've seen here of populating a menu from a database show only the label being populated. Will the control accept both a label and a value such as:
AK 1
AL 2
AR 3
Where the label is a state name but the value returned is an id. The ID would be the primary key on the database table.
Re: Populating menu with label and value
Posted: Thu Feb 18, 2016 12:29 am
by bn
Hi electricclay,
welcome to the Forum.
using a popUp menu on a Mac you can do this
New stack, 1 field, 1 popUp menu button
set the script of the popUp menu to
Code: Select all
on mouseDown
put "AK /|1" & cr & "AL/|2" & cr & "AR/|3" into tTemp
set the text of me to tTemp
end mouseDown
on menuPick pItemName
put pItemName into field 1
end menuPick
what is visible is "AK,AL,AR", the message on menupick sends "1,2,3" respectively.
This should work for Windows too. Can not test. Might even work with other types of Menu-Buttons in Windows.
see "Menu" in the dictionary though the entry is a bit opaque. Just ask here.
Menus are a bit tricky in Livecode because of different Human Interface Guidelines between the Operating systems.
Kind regards
Bernd
Re: Populating menu with label and value
Posted: Thu Feb 18, 2016 12:52 am
by dunbarx
What Bernd said.
But this concept is easy. All buttons have a text property, but only menu style buttons will display that data directly. The menuItems of a menu style button in LC are simply the contents of the button. So in any button of any kind, you can:
Code: Select all
put "X" & return & "Y" & return & "Z" into button "yourButton"
The label of a menu style button is the currently selected menuItem. So to get at that "Y", you could
Code: Select all
set the label of btn "yourButton" to "Y"
or
Set the menuHistory of btn "yourButton" to 2
Same thing, but each has it strengths. Invoking either action sets the property of the other.
In an ordinary button, you would:
Code: Select all
answer line 2 of btn "yourButton"
or even:
set the label of btn "yourButton" to line 2 of btn "yourButton"
Just takes a little practice.
Craig Newman
Re: Populating menu with label and value
Posted: Sun Feb 21, 2016 7:32 pm
by electricclay
Thanks I'll play around with both solutions.