TweetFollow Us on Twitter

Programming Menus
Volume Number:1
Issue Number:3
Column Tag:BASIC School

Programming Menus

By Dave Kelly

This month's BASIC School features programming menus on the MAC. One of the first things I do when I start a program is the menu. This way I'm sure that I know what I want to do in the program and it helps me to organize in a "top-down" fashion. Hopefully, after reading this you will know the basics of menu programming. The best thing is to experiment and test your own programs out using ideas you may gain from reading here.

First a bit of a rundown on the syntax for Programming menus in Microsoft BASIC (version 2.0). The statements we have at our disposal are:

• MENU menu-id, item-id, state   [,title-string]
• MENU 
• MENU RESET

Menu-id is a number from 1 to 10 representing the menu bar selected. Item-id is a number from 1 to 20 representing the menu item selected. If Item-id is 0, the entire menu is selected.

The words menu-id and item-id make good descriptive variable names for the functions they perform. Use 0 for state to disable the menu or menu item, 1 to enable it or 2 to enable it and place a check mark by it. The title-string ( optional ) is a string representing the title of the selected menu bar or the selected item in the menu.

The MENU command used by itself returns the current menu selection to normal black-on-white video. MENU RESET is used to set all the menu bars back to the menus used by BASIC. It is also a way to erase your custom menus when returning back to basic from a program.

Two more functions (called function syntax) are used to poll the status of the menu selection from within a program:

• MENU(0)
• MENU(1)

MENU(0) returns a number corr- esponding to the number of the last menu bar selection. Once it is executed, MENU(0) is reset to 0. MENU(1) returns a number which corresponds to the number of the last menu item selected. The function syntaxes are executed by setting the function equal to a variable (example: Menuid = MENU(0)). In most of the sample programs I have seen the variables Menuid and Itemid are used for MENU(0) and MENU(1) respectively. Any legal variable name could be used but these seem to describe the functions quite well.

OK, now we know all about menu commands. The example program was written to demonstrate the menu functions. I was hungry at the time I started writing the program, so you can see how it came out. The program sets up a lunch "menu" and asks you to select what you would like. If there isn't anything you like, the program could be modified to include just the things you like or even your own routines. Then when you have made which ever combination of lunch selections you may then "Eat it". After that you may return to make more selections, but only those items which have not been previously selected. When everything has been eaten or you are tired of eating, you may stop the program and return to BASIC.

Now a more detailed explaination of how the menu options are controlled in the program. The first 9 menu statements set up my custom menu. Since this is the first time that each item has been used I have given each menu item a name. If no name had been specified, the menu item would have been blank. To erase previous menu items you can use "" (Null) as the title-string option in each menu item you want to erase. Don't erase menus you may need later. It is better to deselect the menu or menu item with the state argument.

The menu-id is set to 6 because I use the 6th menu bar in order to preserve the BASIC menus. The BASIC menus could be written over and the new menu would take its place. The second argument, item-id, is numbered from 0 to 8, representing a different menu item. (A zero represents the entire menu). The state is set to 1 to enable each menu item.

After some setup of variables, the statement ON MENU GOSUB, enables event trapping for the menus. Event trapping will be covered at a later time, however for this case, this statement indicates what will happen as soon as any menu is selected. In this case, the program will jump to the subroutine named 'loop'. However, this event trapping is not enabled until the statement, MENU ON is executed. After the MENU ON statement the program goes into an infinite loop where it will wait for a menu to be selected. You should especially be careful when in infinite loops to be sure that there will eventually be some way to get out of the loop. If you had erased the BASIC menus and then somehow become stuck in a loop someplace, the only way out it the command-".". Beware when error trapping the break key (command-".") using the BREAK statements, there may be now way out of an endless loop without doing a system reset or turning off the power.

The program then jumps to the 'loop' routine. There the variables, MenuId and ItemId are set equal to the functions MENU(0) and MENU(1) so that the program can tell which of the menu items was selected. The ON ItemId GOSUB statement specifies which subroutine the selection will cause the program to branch to.

When the program begins, all of the items are selected, but none have a check mark by them. Take a look at one of the subroutines, say the starting at the label titled "Hamburger". Using labels makes it clear which routine does what, especially if the labels describe what the routine is about. All the routines are similar and will show the same technique. The var- iable 'Hamburger.select' is set to 1 to indicate that the menu is active, with no check mark. If the menu item is selected, the 'Hamburger.select' variable is toggled between 1 and 2 each time it is selected (unless the menu item has been disabled by setting the variable to 0. The MENU 6,3,Hamburger.select statement sets up the condition of the menu, 0 for disabled, 1 for enabled (no checkmark), 2 for enabled with checkmark. The 'Eat it' subroutine will disable any menus which have been enabled with the checkmark. This makes it impossible to eat more than one hamburger. (Fortunately, there is still alot of other things to eat.) If no items have been checked and 'Eat it' is selected from the menu, the entire menu is returned to the normal black-on white video with the MENU statement before exiting from the 'Eat it' routine.

After making the menu selection, the program branches back to the infinite loop where it started before being "event trapped" by the menu selection. When 'Stop' is selected, the program initiates the MENU RESET statement and returns to BASIC.

Since this program was designed for study (it serves no real function, especially since it isn't edible), I would advise that you use the trace and step functions to see just how each statement effects the program. If you want to get really creative you could have the program draw the food for you after each selection.

The program uses the method of looping in an infinite loop while waiting for a menu selection to interupt the loop. Another method which takes much more caution when writing the program involves using Event Trap Programming. This is explained briefly starting on page 68 of the Microsoft BASIC 2.0 manual (Advanced topics). Three commands, MENU ON, MENU OFF, MENU STOP are used liberally to determine when menus can be accessed and what events can occur when they are accessed. The problems occur when several menus call the same routines and use the same variables. When the menu is selected and event trapping is enabled, the menu will interupt the program wherever it is and go run the selected routine, possibly changing variables that were being used when the program was interupted. Event Trap Programming will be covered at a later time, hopefully when more of the basics of programming Macintosh BASIC have been covered. Next time we will open up some windows and explore some more keys to Programming BASIC on the MAC.


'----------------------------
'           Dave's Lunch Menu
'              by Dave Kelly
'      MACTECH  © February 1985
'----------------------------
MENU 6,0,1,"Dave's Menu"  'New menu
MENU 6,1,1,"Sandwich"
MENU 6,2,1,"Potato Chips"
MENU 6,3,1,"Hamburger"
MENU 6,4,1,"Hot Dog"
MENU 6,5,1,"Soda Pop"
MENU 6,6,1,"Ice Cream"
MENU 6,7,1,"Eat it"
MENU 6,8,1,"Stop"
Sandwich.select=1:Potato.select=1:
 Hamburger.select=1:Hot.select=1
 Soda.select=1:Ice.select=1:count=0
PRINT "What's for lunch?"
PRINT"Please Select from Dave's Menu."
ON MENU GOSUB loop
MENU ON
Pause:GOTO Pause   'Wait for menu selection in infinite loop
Loop:
MenuId = MENU(0)   'Menu # selected
ItemId = MENU(1)   'Item # selected
ON ItemId GOSUB Sandwich, Potato.Chips,Hamburger,Hot.Dog, Soda.Pop,Ice.Cream,Eat.it,Stopit
RETURN
Sandwich:
IF Sandwich.select=1 THEN Sandwich.select=2:GOTO Set.Sandwich
IF Sandwich.select=2 THEN Sandwich.select=1
Set.Sandwich:MENU 6,1,Sandwich.select
RETURN
Potato.Chips:
IF Potato.select=1 THEN Potato.select=2:GOTO Set.Potato
IF Potato.select=2 THEN Potato.select=1
Set.Potato:MENU 6,2,Potato.select
RETURN
Hamburger:
IF Hamburger.select=1 THEN Hamburger.select=2:GOTO Set.Ham
IF Hamburger.select=2 THEN Hamburger.select=1
Set.Ham:MENU 6,3,Hamburger.select
RETURN
Hot.Dog:
IF Hot.select=1 THEN Hot.select=2:GOTO Set.Hot
IF Hot.select=2 THEN Hot.select=1
Set.Hot:MENU 6,4,Hot.select
RETURN
Soda.Pop:
IF Soda.select=1 THEN Soda.select=2:GOTO Set.Soda
IF Soda.select=2 THEN Soda.select=1
Set.Soda:MENU 6,5,Soda.select
RETURN
Ice.Cream:
IF Ice.select=1 THEN Ice.select=2:GOTO Set.Ice
IF Ice.select=2 THEN Ice.select=1
Set.Ice:MENU 6,6,Ice.select
RETURN
Eat.it:
PRINT:PRINT"You have just eaten:"
old.count=count
IF Sandwich.select=2 THEN Sandwich.select=0: MENU 6,1,0:PRINT "Sandwich":count=count+1
IF Potato.select=2 THEN Potato.select=0: MENU 6,2,0:PRINT"Potato Chips":count=count+1
IF Hamburger.select=2 THEN Hamburger.select=0:MENU 6,3,0: PRINT"Hamburger":count=count+1
IF Hot.select=2 THEN Hot.select=0:MENU 6,4,0: PRINT"Hot Dog":count=count+1
IF Soda.select=2 THEN Soda.select=0:MENU 6,5,0:PRINT"Soda Pop":count=count+1
IF Ice.select=2 THEN Ice.select=0:MENU 6,6,0:PRINT"Ice Cream":count=count+1
IF count=6 THEN MENU 6,7,0:PRINT"The food is all gone."
IF old.count=count THEN PRINT"Nothing":MENU     ' Deselect current menu 
selection
RETURN
Stopit:MENU RESET      'reset the BASIC menu bars and then END the program
END

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.