Menu |
QBasic GUI Tutorial #3I left you last time with an application looking something like this:
I left you to discover how the frames and textbox was created but in case you were having difficulty, here is a brief outline. From the CreateFrame subroutine there is this description: ' WindowNum - Host window index number ' Num - Index of the frame ' x, y - coords. ' XLen, YLen - X Length, Y Length ' Caption$ - Caption and the code we used to create the frames was: CreateFrame 1, 10, 10, 10, 190, 40,
"Result" So we have the number of the window to create the frame in (we only have a single window at the moment but it still has to be specified which in this case is window 1 (one)). CreateFrame 1, 10, 10, 10, 190, 40,
"Result" Index numbers don't need to be consecutive so I used 10 and 3 respectively. Larger projects may have to be more consistently numbered. CreateFrame 1, 10, 10, 10, 190, 40,
"Result" Then we have the top corner coordinates with the left one the same so they line up and only the second one required to move the second frame down. CreateFrame 1, 10, 10, 10, 190, 40,
"Result" Then there is the width and height settings CreateFrame 1, 10, 10, 10, 190, 40, "Result" and the frame caption (empty strings such as "" will produce an unbroken box). Now on to the text box which is called a text field ( a text box is actually rather confusingly a simple text label). Here is the description: ' WindowNum - Host window number ' Num - Textfield index number ' x, y - Coords. ' Length - Max. characters ' Txt$ - Startup text ' Passwd - If true, then "*" will be displayed instead of a character and the code we used looks like this: CreateTextField 1, 1, 20, 20, 25, "", False So we start once again with the number of the window we wish to place the text field in. CreateTextField 1, 1, 20, 20, 25, "", False Then we have the number of the text field itself to distinguish it from other text fields. CreateTextField 1, 1, 20, 20, 25, "", False Coordinates of where to put it inside the window CreateTextField 1, 1, 20, 20, 25, "", False and the maximum length of string that the box will take (very useful for ensuring that array indexes are not exceeded) is next. CreateTextField 1, 1, 20, 20, 25, "", False Then any text you want to display in the box (set to blank in this instance) and seems to be the only way to get text into a box. CreateTextField 1, 1, 20, 20, 25, "", False If the final setting is set to true then asterisks will be displayed instead of what is actually being entered - very useful for password routines. And now for some more. First, there is a slight modification to be added. Go to the user code area and look for this bit of code: IF ClickedButton = 2 THEN CreateTextField 1, 1, 20, 30, 25, "Hello", False END IF Now modify it to look like this: IF ClickedButton = 2 THEN KillTextField 1 CreateTextField 1, 1, 20, 30, 25, "Hello", False END IF This will ensure that only one text box is created. Now we'll take a look at menu's. In the Initscreen section add the following code: CreateMenu 1, 1, "File" AddMenuItem 1, 1, "Exit" CreateMenu 1, 2, "Help" AddMenuItem 1, 2, "About" This will create two menu's each with a single option. Now in user code add: IF awin = 1 AND menunumber = 1 AND menuselected = 1 THEN Hiiripiiloon SCREEN 0: CLS COLOR 14, 0 PRINT " þ Exit from Windoze " + Version.Major + "." + Version.Minor + " (C) Sami Kyostila 1997" PRINT COLOR 14 PRINT " þ Memory:" COLOR 7 PRINT " String:", COLOR 11 PRINT FRE("") COLOR 7 PRINT " Stack:", COLOR 11 PRINT FRE(-2) COLOR 7 PRINT " Array:", COLOR 11 PRINT FRE(-1) PRINT COLOR 7 END END IF This will close down the program if the File | Exit menu is clicked on. I have come across several problems which means that the code will have to be tweaked a bit. The documentation at the top of the problem says that the keyword MenuActive will give the number of the active menu - it doesn't. It tells the program whether the menu is active or not (true/false). The actual variable should be MenuNumber which is used above. There also appears to be another feature tht needs some extra coding. If the second menu (Help | About) is selected first (even though it does nothing at the moment), if the File Menu is clicked, the program will close down as if File | Exit had been selected which is not what is intended. The only way I can see to cure this (without changing the system code which I'm keen to avoid) is to set the first menu option to a blank. In Win95 it is possible to have a separator line but this is not possible in Windoze so I have to resort to dashes. The code needs to be modified in Initscreen to look like this: CreateMenu 1, 1, "File" AddMenuItem 1, 1, "--------" AddMenuItem 1, 1, "Exit" CreateMenu 1, 2, "Help" AddMenuItem 1, 2, "About" with the dashes as shown. The problem will then not appear. However some of the code will need to be changed from: IF awin = 1 AND menunumber = 1 AND menuselected = 1 THEN to: IF awin = 1 AND menunumber = 1 AND menuselected = 2 THEN which will ensure that nothing happens if the dashes are selected (I have yet to see whether this is a problem in the textmode version). So to understand how menu's work, here is a quick rundown. ' WindowNum - Host window index number ' Number - The number of the menu in the window, ' counting from left to right (1: First, 2: Second etc.) ' Caption$ - Menu caption. and the code we used is: CreateMenu 1, 1, "File" The number of the window is first of course just as before. CreateMenu 1, 1, "File" Then we have the number of each of the menu's so File is first with Help displayed to the right of it. CreateMenu 1, 1, "File" Finally, there is the text or caption of the menu's in quotes. Now we add items to each of the menu's. AddMenuItem 1, 1, "--------" Each menu item does not need to have it's own number. Instead, there is the number of the window. AddMenuItem 1, 1,
"--------" Then there is the number of the menu to attach it to: AddMenuItem 1, 1,
"--------" Finally there are the text captions: AddMenuItem 1, 1, "--------" The first menu (File) has the dummy for it's first item as discussed above then menu 1 has Exit added to it and menu 2 has About. Now to the bit that makes the Exit menu work. The essential bit of code is: IF awin = 1 AND menunumber = 1 AND menuselected = 2 THEN and means that if the active window (awin) is window number one and the number of the menu (menunumber) is also one and the menu selected is menu item number 2 (Exit) then do the code that follows which closes down the program and displays diagnostics. Next time we'll get the About menu working and display a dialog box. Have fun... |
Written by Nick Cheesman.
Last updated: 01/04/2004 Please eMail me at: nickjc@nickjc.co.uk |