QBasic GUI Tutorial

Menu

Back to tutorial menu

QBasic GUI Tutorial #4

Last time, we added menu's to our Windoze application but it had an odd side effect which we must now rectify. Chnage the code in Initscreen from:

CreateFrame 1, 10, 10, 10, 190, 40, "Result"
CreateTextField 1, 1, 20, 20, 25, "", False

to:

CreateFrame 1, 10, 10, 20, 190, 30, "Result"
CreateTextField 1, 1, 20, 30, 25, "", False

and all will be well. It probably would be wise to design your applications with menu's in mind to avoid this problem arising again. Now to get that About box working. Windoze has a built in dialog window that can be used for this purpose although it is a little basic and lacks the icons that Win95 has in it's message boxes but it is useable. It is created on the fly (at run time) so all the code goes in the user code area.

IF awin = 1 AND menunumber = 2 AND menuselected = 1 THEN
  Dialog "About", "Windoze created by Sami Kyostila. 
  Tutorials by Nick Cheesman 2003.", "OK", "", ""
  menunumber = 1
END IF

Here again we are looking for the window (awin), number of the menu (menunumber) and responds to menu item 1 (menuselected) which will then display the dialog. The code setting the menunumber to 1 is a tweak needed otherwise the dialog will simply appear again and you will be unable to close the program down (dialogs must be shut down before the main window can be clicked on). The dialog command consists of 5 separate commands each separated by a comma.

' Topic$ - Window topic
' Text$ - Displayed text
' Button1$ - Button 1 text
' Button2$ - Button 2 text (if blank, then button won't be created)
' Button3$ - Button 3 text (if blank, then button won't be created)

This creates a new window and it is assigned the last window number (in this case window 10) so that cannot be used by your own program. We give it a window caption (or topic) followed by a comma. Then we add the text to be displayed which will be word wrapped. The first button is assigned the text OK and the last two buttons are set to empty strings and so are not displayed (they will be explained in a later tutorial).

So there only remains a bit of code to tidy up. That splash screen can be removed as follows. Delete the subroutine called Splash. Search for the following code:

'-----------------------------------------------------------------
' Show splash screen
'-----------------------------------------------------------------

Hiiriesiin
splash

and delete the keyword Splash. Then search for:

'-----------------------------------------------------------------
' Quote of the day
'-----------------------------------------------------------------

Quotes:

'Number of quotes
DATA 22

and delete all the data statements (to save space). You could of course simply modify the Splash subroutine to give a screen more to your liking and add modify the quote of the day so that it isn't just for programmers.

So that completes (for the moment) for first Windoze application. However, what if you wanted to distribute this file to other users. It's a bit fiddly getting them to load the BAS file into QBasic and then run it and then shut down the editor when finished. Wouldn't it be better to have it perform more like a real application. Well, you can. First create a text file in your QBasic folder called:

run.bat

and open it in an editor. Then add the lines:

@echo off
qbasic /run guiapp.bat

where guiapp.bas is your own .bas file. Running the batch file as if is a program will start QBasic but it will go straight into your application without waiting for the user to press F5. However, clicking on the Exit button brings the editor back again. Time to add a bit of extra code. In user code of you program change:

  IF awin = 1 AND menunumber = 1 AND menuselected = 2 THEN
    Hiiripiiloon
    SCREEN 0: CLS
    COLOR 14, 0
    PRINT " þ Exit from Windoze " + Version.Major + "." 
+ Version.Minor + " (C) Sami Ky”stil„ 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

  IF ClickedButton = 7 THEN
    Hiiripiiloon
    SCREEN 0: CLS
    COLOR 14, 0
    PRINT " þ Exit from Windoze " + Version.Major + "." 
+ Version.Minor + " (C) Sami Ky”stil„ 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

to:

IF awin = 1 AND menunumber = 1 AND menuselected = 2 THEN
  Hiiripiiloon 
  SCREEN 0: CLS
  SYSTEM
  END
END IF

IF ClickedButton = 7 THEN
  Hiiripiiloon
  SCREEN 0: CLS
  SYSTEM
END
END IF

The imporatnt keyword is SYSTEM which will close QBasic down. If you are using this in Windows, ensure that the DOS box is set to shutdown automatically and your users need never know it's a QBasic program. That completes this tutorial. Watch out for some more soon...

  Written by Nick Cheesman. Last updated: 01/04/2004
Please eMail me at: nickjc@nickjc.co.uk