Dynamic card creation navigation menu
This lesson demonstrates how to dynamically update a navigation menu as cards are created.
The menuPick message
The menuPick message is sent when the user chooses a menu item from the menu. Remember that in LiveCode, menus are usually implemented as buttons: the button's style and menuMode properties control how it is displayed, and the button's text property is used as the list of menu items. When the user chooses an item from the menu, the menuPick message is sent to the button, triggering this handler.
Setting up the menu
The menu is initially set up to contain menu items for going to the previous or next cards, and for creating a new card, with appropriate separators. Once you have set up the menu, creating a card automatically adds the new card's name to the menu. In this example, the initial text of the menu button, which you can enter in the button's properties pane, looks like this:
Previous Student
Next Student
(-
New Student
(-
As new cards are created, the list of students will be placed at the end of the menu, after the last "(-" separator line.
The menuPick handler
The menuPick handler does different things depending on the menu item. If the user chooses "Previous Student" or "Next Student", the handler simply goes to the previous or next card.
If the user chooses "New Student", the handler requests the student's name, then creates a card with that name, and adds the new card's name to the end of the menu (after the separator line). When a user chooses one of the card names, the handler goes to that card.
The default keyword is useful here because we don't know the wording of all the menu items when writing the handler. Using a switch statement, we can set up a case for each of the menu items we know about, "Previous Student", "Next Student", and "New Student", and write a default case to handle all other possibilities in a generic way.
The menuPick handler code
on menuPick pItem
local tName
switch pItem
case "Previous Student"
go previous card
break
case "Next Student"
go next card
break
case "New Student"
## create a new card and put its name in the menu:
ask "Please enter the student's name:"
if it is empty then
exit menuPick
end if
put it into tName
lock screen
go last card ## so the new student is placed at the end
create card tName ## card's name is student name entered above
put return & tName after me ## add name to bottom of menu
unlock screen
default
## if none of the previous cases apply, it's a card:
if there is a card pItem then
go card pItem
else
beep
end if
end switch
end menuPick
0 Comments
Add your comment