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

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple 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.