Populating menu with label and value

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
electricclay
Posts: 2
Joined: Wed Feb 17, 2016 10:38 pm

Populating menu with label and value

Post by electricclay » Wed Feb 17, 2016 11:04 pm

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.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Populating menu with label and value

Post by bn » Thu Feb 18, 2016 12:29 am

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

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

Re: Populating menu with label and value

Post by dunbarx » Thu Feb 18, 2016 12:52 am

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

electricclay
Posts: 2
Joined: Wed Feb 17, 2016 10:38 pm

Re: Populating menu with label and value

Post by electricclay » Sun Feb 21, 2016 7:32 pm

Thanks I'll play around with both solutions.

Post Reply