Option Menu - Labels & Values

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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
igorBUSvRhc
Posts: 37
Joined: Thu Apr 19, 2012 8:12 am

Option Menu - Labels & Values

Post by igorBUSvRhc » Mon May 21, 2012 7:18 am

Hi all!

Often when we show an option menu, the value that the user 'picks' from the menu is not actually the value we want stored. For instance, I have an option menu that shows the names of the countries of the world for the user to pick. However, I do not want to store the full name of the countries as the choice - I need to store a 2-letter country code instead.

Is there a 'neat' way to store both the "label" and the "value" in the option menu itself, like we do with HTML? Or do we have to write a private function that 'converts' between the label and the value upon 'menuPick'? - that seems to be a lot less easy...

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: Option Menu - Labels & Values

Post by shaosean » Mon May 21, 2012 11:15 am

For menus you would normally use a "tag", but it looks like this feature was not implemented on option menus..
The menu item specification has been extended to allow a tag to be specified. It is now of the form:
<label> [ '/' [ <accelerator> ] [ '|' <tag> ] ]
Note that the <tag> is optional as is the <accelerator> however, if you want an item with a tag but without the accelerator you need:
<label> '/' '|' <tag>

The <tag> must only be composed of characters from the ASCII character set.

If a menu item has a <tag> then it is the tag string that is passed to menuPick rather than the <label>. This is useful for localization of menu items as you don't need to change the menuPick handler for each language supported.

The following tags should be used to label the standard edit menu items to enable them to be controlled by the system dialogs on Mac OS X (e.g. answer file, ask file etc.):
undo, redo, cut, copy, paste, clear, select all, preferences

Note: You should not mix the use of tags and no tags in Unicode menus. Doing so will cause empty values for all unicode items without tags.

Randy Hengst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 148
Joined: Thu Jun 29, 2006 4:16 pm

Re: Option Menu - Labels & Values

Post by Randy Hengst » Mon May 21, 2012 3:26 pm

You might include the two-letter as the last work of the label... then do something like this in the option button script... using the -1 will then work for one word and multi-word country names.


on menuPick pItemName
switch pItemName
default
put word -1 of pItemName
break
end switch
end menuPick

be well,
randy hengst

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7019
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Option Menu - Labels & Values

Post by dunbarx » Mon May 21, 2012 3:30 pm

Hi,

Store the country codes in a custom property (say, "countryCodes") of the option menu, one for each of the country entries. Use the menuHistory to determine the menuItem selected.

Code: Select all

on menuPick pItemName
   switch pItemName
      case "someCountry"
         answer line the menuhistory of me of the countryCodes of me
   end switch
end menuPick
You can also just use a brute force look-up, based on the name of the country chosen itself, also stored in a custom property, but the first seems to me more compact.

Craig Newman

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”