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

Combo Quest (Games)
Combo Quest 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: Combo Quest is an epic, time tap role-playing adventure. In this unique masterpiece, you are a knight on a heroic quest to retrieve... | Read more »
Hero Emblems (Games)
Hero Emblems 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: ** 25% OFF for a limited time to celebrate the release ** ** Note for iPhone 6 user: If it doesn't run fullscreen on your device... | Read more »
Puzzle Blitz (Games)
Puzzle Blitz 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Puzzle Blitz is a frantic puzzle solving race against the clock! Solve as many puzzles as you can, before time runs out! You have... | Read more »
Sky Patrol (Games)
Sky Patrol 1.0.1 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0.1 (iTunes) Description: 'Strategic Twist On The Classic Shooter Genre' - Indie Game Mag... | Read more »
The Princess Bride - The Official Game...
The Princess Bride - The Official Game 1.1 Device: iOS Universal Category: Games Price: $3.99, Version: 1.1 (iTunes) Description: An epic game based on the beloved classic movie? Inconceivable! Play the world of The Princess Bride... | Read more »
Frozen Synapse (Games)
Frozen Synapse 1.0 Device: iOS iPhone Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: Frozen Synapse is a multi-award-winning tactical game. (Full cross-play with desktop and tablet versions) 9/10 Edge 9/10 Eurogamer... | Read more »
Space Marshals (Games)
Space Marshals 1.0.1 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0.1 (iTunes) Description: ### IMPORTANT ### Please note that iPhone 4 is not supported. Space Marshals is a Sci-fi Wild West adventure taking place... | Read more »
Battle Slimes (Games)
Battle Slimes 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: BATTLE SLIMES is a fun local multiplayer game. Control speedy & bouncy slime blobs as you compete with friends and family.... | Read more »
Spectrum - 3D Avenue (Games)
Spectrum - 3D Avenue 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: "Spectrum is a pretty cool take on twitchy/reaction-based gameplay with enough complexity and style to stand out from the... | Read more »
Drop Wizard (Games)
Drop Wizard 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Bring back the joy of arcade games! Drop Wizard is an action arcade game where you play as Teo, a wizard on a quest to save his... | Read more »

Price Scanner via MacPrices.net

Our MacBook Price Trackers will show you the...
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Amazon is offering a 10% discount on Apple’s...
Don’t pay full price! Amazon has 16-inch M4 Pro MacBook Pros (Silver and Black colors) on sale today for 10% off Apple’s MSRP. Shipping is free. These are the lowest prices currently available for 16... Read more
13-inch M4 MacBook Airs on sale for $150 off...
Amazon has new 13″ M4 MacBook Airs on sale for $150 off MSRP right now, starting at $849. Sale prices apply to most colors and configurations. Be sure to select Amazon as the seller, rather than a... Read more
15-inch M4 MacBook Airs on sale for $150 off...
Amazon has new 15″ M4 MacBook Airs on sale for $150 off Apple’s MSRP, starting at $1049. Be sure to select Amazon as the seller, rather than a third-party: – 15″ M4 MacBook Air (16GB/256GB): $1049, $... Read more
Amazon is offering a $50 discount on Apple’s...
Amazon has Apple’s 11th-generation A16 iPads in stock on sale for $50 (or a little more) off MSRP this week. Shipping is free: – 11″ 11th-generation 128GB WiFi iPads: $299 $50 off MSRP – 11″ 11th-... Read more
Clearance 13-inch M1 MacBook Airs available f...
Walmart has clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) available online for $649, $360 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks for... Read more
iPad minis on sale for $100 off Apple’s MSRP...
Amazon is offering $100 discounts (up to 20% off) on Apple’s newest 2024 WiFi iPad minis, each with free shipping. These are the lowest prices available for new minis among the Apple retailers we... Read more
AirPods Max headphones on sale for $479, $70...
Amazon has AirPods Max with USB-C on sale for $479.99 in all colors. Shipping is free. Their price is $70 off Apple’s MSRP, and it’s the lowest price available today for AirPods Max. Keep an eye on... Read more
14-inch M4 Pro/M4 Max MacBook Pros on sale th...
Don’t pay full price! Get a new 14″ MacBook Pro with an M4 Pro or M4 Max CPU for up to $320 off Apple’s MSRP this weekend at these retailers…they are the lowest prices available for these MacBook... Read more
Get a 15-inch M4 MacBook Air for $150 off App...
A couple of Apple retailers are offering $150 discounts on new 15″ M4 MacBook Airs this weekend. Prices at these retailers start at $1049: (1): Amazon has new 15″ M4 MacBook Airs on sale for $150 off... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.