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

Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links... | Read more »
Price of Glory unleashes its 1.4 Alpha u...
As much as we all probably dislike Maths as a subject, we do have to hand it to geometry for giving us the good old Hexgrid, home of some of the best strategy games. One such example, Price of Glory, has dropped its 1.4 Alpha update, stocked full... | Read more »
The SLC 2025 kicks off this month to cro...
Ever since the Solo Leveling: Arise Championship 2025 was announced, I have been looking forward to it. The promotional clip they released a month or two back showed crowds going absolutely nuts for the previous competitions, so imagine the... | Read more »
Dive into some early Magicpunk fun as Cr...
Excellent news for fans of steampunk and magic; the Precursor Test for Magicpunk MMORPG Crystal of Atlan opens today. This rather fancy way of saying beta test will remain open until March 5th and is available for PC - boo - and Android devices -... | Read more »
Prepare to get your mind melted as Evang...
If you are a fan of sci-fi shooters and incredibly weird, mind-bending anime series, then you are in for a treat, as Goddess of Victory: Nikke is gearing up for its second collaboration with Evangelion. We were also treated to an upcoming... | Read more »
Square Enix gives with one hand and slap...
We have something of a mixed bag coming over from Square Enix HQ today. Two of their mobile games are revelling in life with new events keeping them alive, whilst another has been thrown onto the ever-growing discard pile Square is building. I... | Read more »
Let the world burn as you have some fest...
It is time to leave the world burning once again as you take a much-needed break from that whole “hero” lark and enjoy some celebrations in Genshin Impact. Version 5.4, Moonlight Amidst Dreams, will see you in Inazuma to attend the Mikawa Flower... | Read more »
Full Moon Over the Abyssal Sea lands on...
Aether Gazer has announced its latest major update, and it is one of the loveliest event names I have ever heard. Full Moon Over the Abyssal Sea is an amazing name, and it comes loaded with two side stories, a new S-grade Modifier, and some fancy... | Read more »
Open your own eatery for all the forest...
Very important question; when you read the title Zoo Restaurant, do you also immediately think of running a restaurant in which you cook Zoo animals as the course? I will just assume yes. Anyway, come June 23rd we will all be able to start up our... | Read more »
Crystal of Atlan opens registration for...
Nuverse was prominently featured in the last month for all the wrong reasons with the USA TikTok debacle, but now it is putting all that behind it and preparing for the Crystal of Atlan beta test. Taking place between February 18th and March 5th,... | Read more »

Price Scanner via MacPrices.net

AT&T is offering a 65% discount on the ne...
AT&T is offering the new iPhone 16e for up to 65% off their monthly finance fee with 36-months of service. No trade-in is required. Discount is applied via monthly bill credits over the 36 month... Read more
Use this code to get a free iPhone 13 at Visi...
For a limited time, use code SWEETDEAL to get a free 128GB iPhone 13 Visible, Verizon’s low-cost wireless cell service, Visible. Deal is valid when you purchase the Visible+ annual plan. Free... Read more
M4 Mac minis on sale for $50-$80 off MSRP at...
B&H Photo has M4 Mac minis in stock and on sale right now for $50 to $80 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses: – M4 Mac mini (16GB/256GB): $549, $50 off... Read more
Buy an iPhone 16 at Boost Mobile and get one...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering one year of free Unlimited service with the purchase of any iPhone 16. Purchase the iPhone at standard MSRP, and then choose... Read more
Get an iPhone 15 for only $299 at Boost Mobil...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering the 128GB iPhone 15 for $299.99 including service with their Unlimited Premium plan (50GB of premium data, $60/month), or $20... Read more
Unreal Mobile is offering $100 off any new iP...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 14, 13, and SE... Read more
Apple drops prices on clearance iPhone 14 mod...
With today’s introduction of the new iPhone 16e, Apple has discontinued the iPhone 14, 14 Pro, and SE. In response, Apple has dropped prices on unlocked, Certified Refurbished, iPhone 14 models to a... Read more
B&H has 16-inch M4 Max MacBook Pros on sa...
B&H Photo is offering a $360-$410 discount on new 16-inch MacBook Pros with M4 Max CPUs right now. B&H offers free 1-2 day shipping to most US addresses: – 16″ M4 Max MacBook Pro (36GB/1TB/... Read more
Amazon is offering a $100 discount on the M4...
Amazon has the M4 Pro Mac mini discounted $100 off MSRP right now. Shipping is free. Their price is the lowest currently available for this popular mini: – Mac mini M4 Pro (24GB/512GB): $1299, $100... Read more
B&H continues to offer $150-$220 discount...
B&H Photo has 14-inch M4 MacBook Pros on sale for $150-$220 off MSRP. B&H offers free 1-2 day shipping to most US addresses: – 14″ M4 MacBook Pro (16GB/512GB): $1449, $150 off MSRP – 14″ M4... Read more

Jobs Board

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