Custom PopUpWindows for Your Program

by Welopez

Just Basic gives you several windows similar to a popup, which can easily be added to your program; NOTICE, PROMPT, and CONFIRM. What do you do if these are not enough and you want to create a PopUp window with a custom error message for your user, or get a short text answer? No problem....! The complete DEMO can be downloaded by clicking on the link for the archive zip.

In this DEMO, we're going to load assorted text messages from DATA statements to an array, popMsg$(). Because JB does not permit arrays to be loaded directly from DATA statements, it is first necessary to read them to a dummy$ before assigning the message to the array.


DIM popMsg$(29)  'There are 29 data statements to be loaded into popMsg$() array.
FOR k=1 TO 29
READ dummy$  'Use a temp value when loading from DATA statements.
popMsg$(k)=dummy$
NEXT k

If your program has fewer, or more, DATA statements, adjust the FOR/NEXT loop and dimension of popMsg$() to suit your purpose. The messages to be printed could have been read from a text file using LINE INPUT, but I didn't want to create another file to be downloaded for this DEMO.

The next thing we'll do in this DEMO, is open a window, which could be the main window of your program or game. Janet taught me a really neat trick using the TRAPCLOSE statement to call a SUB and close the window, which I've used here.


PRINT #main, "trapclose endPopup"  '<< No brackets needed to CALL a SUB

...and the called subroutine to close the window looks like this.


SUB endPopup handle$  'SUB closes the window and ends the program
    CLOSE #handle$
    END
END SUB

handle$ is the name of the calling window, and JB knows which window, so you don't have to type that into the code. The SUB closes #handle$ and ENDs the program, so it really doesn't get returned to the program, but END SUB is required to complete the SUB.

Our GUI for the main window has a button to bring up a PopUp message. In your program, you could use an event, a comparison, or other action to call the PopUp. The PopUp window used in this DEMO is a WINDOW FOR DIALOG, allowing me to create a hidden button off screen and designate it as the default button for the window. Hitting ENTER will close the window without the need for a mouse move or mouse click.

Pressing Show Popup branches to [rndMsg] to select which of the messages in popMsg$() array will be printed. If you are using events to call your PopUp, you will not need this random routine. When a message number has been chosen, CALL showPopup is executed and the msg number is passed to the SUB.


[rndMsg]
msg=INT(RND(0)*29)+1  'Choose a random message number for the array
CALL showPopup msg  'Call the popup window and pass the message number to the SUB

SUB showPopup defines the WINDOW FOR DIALOG which will be used to display a random message in a STATICTEXT box. I chose statictext because it has auto-wrap built in and there is no need to worry about the string length, providing your STATICTEXT is wide enough and high enough for the entire message. No button is needed to close the PopUp because the user can hit ENTER and the hidden default button will branch to [endMsg] where it closes the PopUp and returns to the main program.


SUB showPopup num  'SUB to open a Window for DIALOG and display message
WindowWidth=450
WindowHeight=150
UpperLeftX=INT((DisplayWidth-WindowWidth)/2)
UpperLeftY=INT((DisplayHeight-WindowHeight)/2)

STATICTEXT #pop.st1, "", 20, 20, 410, 100
BUTTON #pop.default, "", [endMsg], UL, -20, -20  'Hide the button off screen

OPEN "Your Custom Message" FOR DIALOG AS #pop
PRINT #pop, "trapclose [endMsg]"
PRINT #pop.st1, "!font times_new_roman 16"
    PRINT #pop.st1, popMsg$(num)
WAIT
[endMsg]
    CLOSE #pop
END SUB

Every time you click Show Popup, the random routine selects another message from popMsg$() array and prints it to the PopUp. I chose most of these messages from "Time Enough For Love," by Robert Heinlein, but I also threw in one or two of my own. Once in awhile, as random number generators will do, it generates the same random number more than once. Such is the perversity of rolling the dice.

These are some really neat tricks to add to your JB toolbox for use in programs and games. A WINDOW FOR DIALOG can contain all the usual controls, a TEXTBOX to get user input, a GRAPHICBOX if you want to print a BMP, a LISTBOX, COMBOBOX, RADIOBUTTON, even a TEXTEDITOR if you like, but don't use a default button for your window if you are using a TEXTEDITOR. If the user hits enter at the end of a line of text, the PopUp will branch to the default action. You cannot add a menu to a WINDOW FOR DIALOG. See the help file for Window Types. See also Controls for the syntax to add these features to your window.

Now that you know how easy it is to open a new window to display information to the user, you can scatter these windows throughout your program, maybe to add a little humor or display error messages or other text and graphics.