Select Items


The first part of the code is an example of how to use the function, and the second part is the code itself. Useful example for file or item selection in programs.

(blatant plug) flyingsoft.phatcode.net


DECLARE FUNCTION optionSelect% (array1() AS STRING, x AS INTEGER, y AS INTEGER, unselectedColor AS INTEGER, selectedColor AS INTEGER, rollover AS INTEGER)

'Na__th__an's code, modified a little:
DIM optionList%(1 TO 1)
CLS
CHDIR "C:\qbasic\myprogs\pacman\"
SHELL "dir *.txt /b >list.dir"
f% = FREEFILE
optionAmount% = 0
OPEN "list.dir" FOR INPUT AS #f%
DO
LINE INPUT #f%, file$
options$ = options$ + file$ + "|"
optionAmount% = optionAmount% + 1
LOOP UNTIL EOF(f%)
CLOSE #f%
KILL "list.dir"

'Agamemnus's code:
REDIM optionList$(1 TO optionAmount%)
FOR i% = 1 TO optionAmount%
filenameBreak% = INSTR(options$, "|")
optionList$(i%) = MID$(options$, 1, filenameBreak% - 1)
options$ = MID$(options$, filenameBreak% + 1)
NEXT i%

a% = optionSelect(optionList$(), 4, 4, 2, 4, 1)
IF a% <> -1 THEN PRINT : PRINT : PRINT "Selected " + optionList$(a%)



FUNCTION optionSelect% (array1() AS STRING, x, y, unselectedColor AS INTEGER, selectedColor AS INTEGER, rollover AS INTEGER)

'If the result of the function is -1, then escape (cancel!) option has been
'used.

DIM startOption AS INTEGER, endOption AS INTEGER
DIM currentChoice AS INTEGER, i%, i$

startOption = LBOUND(array1)
endOption = UBOUND(array1)
i$ = CHR$(0) + "P"

DO

'Loop to skip "".
DO: IF i$ <> "" THEN EXIT DO
i$ = INKEY$: LOOP

'Query for key.
SELECT CASE i$

CASE CHR$(0) + "H", CHR$(0) + "K" 'up and left arrow keys
currentChoice = currentChoice - 1
IF currentChoice < startOption THEN
IF rollover = 1 THEN currentChoice = endOption ELSE currentChoice = startOption
END IF

CASE CHR$(0) + "P", CHR$(0) + "M"
currentChoice = currentChoice + 1 'down and right arrow keys
IF currentChoice > endOption THEN
IF rollover = 1 THEN currentChoice = startOption ELSE currentChoice = endOption
END IF

CASE CHR$(27): optionSelect = -1: EXIT DO 'Escape key
CASE CHR$(13): optionSelect = currentChoice: EXIT DO 'Enter key
END SELECT
i$ = ""

FOR i% = startOption TO endOption
IF i% = currentChoice THEN COLOR selectedColor ELSE COLOR unselectedColor
LOCATE i% + y, x: PRINT array1$(i%);
NEXT i%

LOOP

END FUNCTION


SelectItem - page last edited 2010-01-25 10:00:24 by 75.67.131.67 (home) (edit)
Blast WIKI - by RoboticBoy - edited and tweaked for our evil purposes by Hexadecimal Disaster