Here is an annotated listing of the program LOTTO.

Firstly all GEM programs should start by DEFINT so that all variables
are integers unless you specifically want a long or float.
        DEFINT a-z

Next we $include the parts of the GEM toolkit that we will use, here 
we will need a menu bar and dialog handling as well as the all purpose 
routines, SPEED TIP puts the toolbox files into a ramdisk and points the 
enviroment setting to the files.

        REM $include toolbox.bas
        REM $include menu.bas
        REM $include dialog.bas

Next we specify the options to be used by the program, y stops the 
system from automatically opening a window as we don't need one, k50 
tells the system that we want our program to keep 50k of memory for 
our use and return the rest back to GEMDOS, this is being very 
generous as the figure of 50k is in addition to the length of the 
actual program and is used for the stack/variables etc.
The header file from WERCS is also loaded at this point, all the 
objects that you give a name to are listed in this file as CONSTANTS 
ready for use by your program.

        REM $option y,k50
        REM $include a:\lotto\lotto.bh

Now we have all the setting up done, we can start the actual program 
code, first we need somewhere to store the numbers as they are 
generated, all numbers will be stored as strings so that placing them 
into the dialog box is simple, so we dimension an array of 6 strings 
and we make it shared so that any sub-programs have access to it.

        DIM SHARED num$(6)

Now come the sub-programs, I always write the subs first and then the 
main calling code at the end of the listing.

The first sub has got to pick the numbers, making sure that the same 
number cannot be chosen twice, this is done using the built-in random 
number generator, NOTE without the RANDOMIZE TIMER statement the 
numbers chosen each time the program is run will follow the same 
pattern { not very random :) }, the temp array is used to ensure the 
numbers are all different, when a number is chosen we check the array, 
if it holds a 1 then that number has already been selected so pick 
another random number.

Then the number is converted into a string using the FORMATI$ command 
which ensures that two digits are used even if the number is less than 
10, automatically placing a 0 in front if required.

The FOR-NEXT loop does this for 6 numbers, then the sort routine is 
called.

        SUB choose
        STATIC i,d,d$
        LOCAL temp(50)
        RANDOMIZE TIMER
        FOR i=1 TO 6
          DO
          d=(RND*48)+1
          LOOP UNTIL temp(d)=0
          temp(d)=1
          d$=FORMATI$(d,"##")
          num$(i)=d$
        NEXT i
        sort
        END SUB

This is a simple sort routine, using the brutal approach, guaranteed 
to work but can be very slow with large amounts of data, as we only 
have 6 strings here it is quick enough for our needs.

All it does is to compare each entry with all the entries above itself 
and when it finds one in the wrong place swap the two entries around, 
done in a double loop this will produce a sorted list.

        SUB sort
        LOCAL x,y
        FOR x=1 TO 6
        FOR y=x TO 6
        IF VAL(num$(x))>VAL(num$(y)) THEN SWAP num$(x),num$(y)
        NEXT y
        NEXT x
        END SUB

Now we can store the strings in the dialog box structure, the GEM 
toolkit routine SETTE_PTEXT is used for this, the routine requires two 
parameters, the object name {from WERCS}, and the new string to store.
Make certain that the proper TREE is selected, I do that before 
calling this routine {see CASE MNPICK below}.

        SUB store
        sette_ptext first,num$(1)
        sette_ptext second,num$(2)
        sette_ptext third,num$(3)
        sette_ptext fourth,num$(4)
        sette_ptext fifth,num$(5)
        sette_ptext sixth,num$(6)
        END SUB

Using the HISOFT GEM TOOLKIT you have got to write a routine which 
will handle the menu selections, this is not as difficult as it would 
seem because the parameters passed give you all the information that 
you need,  WIND - holds the current window if any,
           ITEM - holds the menu item number,
          TITLE - holds the menu bar title number,

and because we have named all the menu titles & items, we can use 
these names to compare with the parameters to see what was selected, a 
simple CASE structure then does any actions required for that 
selection.

You need a CASE statement for each ITEM, everything after a CASE 
statement up to the next CASE or END SELECT will be executed only if 
that case is true.

        SUB processusermenus (VAL wind,VAL item,VAL title)
        STATIC dum,mem&
        SELECT CASE item
          CASE mnabout
            selecttree about
            mem&=FRE(" ")
            sette_ptext memory%,"memory"+STR$(mem&)
            dum=handledialog(0)
          CASE mnpick
            selecttree pick
            DO
            choose
            store
            dum=handledialog(0)
            LOOP UNTIL dum=pickexit
        END SELECT
        END SUB

The actual HISOFT GEM TOOLKIT startup procedure is basically the same 
for all programs.

        startprogram "a:\lotto\lotto.rsc",menu,mnquit

        hgtloop

        stopprogram

NOTE: in the about dialog I have used a little trick to help the 
      programmer decide how much memory to specify in the $OPTION 
      statement, within the dialog is an object of TEXT type, this is 
      modified each time the dialog is used to show the current amount 
      of free heap space after a garbage collect, you can then alter 
      the $OPTION keep statement to reduce this to a reasonable level, 
      the users of multitasking operating systems will appreciate this 
      effort as your program will not hog all the RAM.
