' Select item from list '====================== DECLARE SUB SelectItem (txt$, l$(), xpos, ypos) DECLARE SUB CBox (x1%, y1%, x2%, y2%, c%) CONST w = 640, maxitems = 25 ' screens width & max fields for a$() COLOR 10, 1: CLS PRINT "Heres an example of creating and displaying a listbox "; PRINT "for the user to select items from." PRINT : PRINT "Use CURSORS and press RETURN to select" PRINT "Press ESC to quit" PRINT : PRINT : PRINT "Continue ... (Press a key)" SLEEP '=================================================== ' Build each list and let user choose a selection '=================================================== DIM a$(maxitems), opt$(4) GOSUB makelist: index = 1 'start at item 1 SelectItem "Choose Size", a$(), 15, 6 IF index = 0 THEN GOTO quit opt$(1) = a$(index) GOSUB makelist: index = 2 'start at item 2 SelectItem "Choose Looks", a$(), 26, 12 IF index = 0 THEN GOTO quit opt$(2) = a$(index) GOSUB makelist: index = 0 'force random selection SelectItem "Now a colour", a$(), 18, 14 IF index = 0 THEN GOTO quit opt$(3) = a$(index) GOSUB makelist: index = 999 'force last item selection SelectItem "Finally, One of these", a$(), 37, 5 IF index = 0 THEN GOTO quit opt$(4) = a$(index) '======================== ' Show selections made '======================== COLOR 9, 1: CLS : LOCATE 3, 18: PRINT "Here are your selections:"; COLOR 14, 1: LOCATE 7, 2 PRINT "You chose a "; opt$(1); " "; opt$(2); " "; opt$(3); " "; opt$(4) END quit: COLOR 9, 1: CLS : COLOR 7, 1: LOCATE 7, 2 PRINT "You pressed the ESC key" END '======================== ' Read data into a$() '======================== makelist: FOR d = 0 TO maxitems: a$(d) = "": NEXT d d = 0 DO d = d + 1: READ a$(d) LOOP UNTIL a$(d) = "END" a$(d) = "" RETURN '============= ' List data '============= DATA "Tiny","Small","Plump","Large","Very Large","Enormous","END" DATA "Nice","Ugly","Pretty","Greasy","Attractive","END" DATA "Red","Green","Yellow","Pink","Blue","Grey","Brown","END" DATA "Dinosaur","Elephant","Cat","Snake","Flea","Horse","Fish","END" SUB CBox (x1%, y1%, x2%, y2%, c%) LOCATE y1%, x1%: COLOR c% wide% = x2% - x1%: high% = y2% - y1% IF wide% < 2 OR high% < 2 THEN EXIT SUB PRINT CHR$(201); STRING$(wide% - 2, 205); CHR$(187); FOR d% = 1 TO high% - 1 LOCATE y1% + d%, x1%: PRINT CHR$(186); LOCATE y1% + d%, x1% + wide% - 1: PRINT CHR$(186); NEXT d% LOCATE y2%, x1% PRINT CHR$(200); STRING$(wide% - 2, 205); CHR$(188); END SUB SUB SelectItem (txt$, l$(), xoffs, yoffs) ' ' txt$ - Text message at top of screen ' l$() - The data array containing list items ' xoffs - X offest pos for list ' yoffs - Y offset pos for list ' ' (NOTE: Surrounding box starts 1 character UP and ' 1 character LEFT of coordinates xoffs & yoffs) ' ' use CURSORS to move through list & press RETURN or ESC ' index - indicates the line selected (after RETURN is pressed) ' This will be zero if ESC is pressed SHARED index bkg = 7 COLOR 11, bkg: CLS LOCATE 1, ((w - LEN(txt$)) / 2) / 8: PRINT TAB(25); txt$ COLOR 1: mx = 0: num = 0 ' count and display list items DO num = num + 1 IF l$(num) = "" THEN EXIT DO LOCATE yoffs + num, xoffs: PRINT l$(num) IF LEN(l$(num)) > mx THEN mx = LEN(l$(num)) LOOP num = num - 1 IF num <= 1 THEN EXIT SUB CBox xoffs - 1, yoffs + 0, xoffs + mx + 1, yoffs + num + 1, 8 RANDOMIZE TIMER IF index < 1 THEN index = RND * num + 1 IF index > num THEN index = num DO: LOOP UNTIL INKEY$ = "" DO COLOR 15, 4: LOCATE index + yoffs, xoffs: PRINT l$(index) 'Highlight Line DO: k$ = INKEY$: LOOP UNTIL k$ <> "" COLOR 1, bkg: LOCATE index + yoffs, xoffs: PRINT l$(index) 'Restore Line SELECT CASE k$ CASE CHR$(0) + "H" 'Curs Up index = index - 1: IF index < 1 THEN index = num CASE CHR$(0) + "P" 'Curs Down index = index + 1: IF index > num THEN index = 1 CASE CHR$(13) 'Return sel = index END SELECT LOOP UNTIL k$ = CHR$(27) OR sel > 0 IF sel = 0 THEN index = 0 END SUB