
build a search function similar to Microsoft Help in Windows 95
Quote:
>I would like to build a search function that scrolls through a list as I
>key in letter after letter. Similar to Microsoft Help in Windows 95
>under Index.
First of all your list HAS to be in alpha/numeric order. Then you need two
variables.... Srch$ and SrchLen%.
A binary search routine is handy here.
FUNCTION fBinSearch%(A$(), L%, Srch$ )
LOCAL F%, M%
'???????????????
F% = 1 '3 start here
DO '3 searching
M% = ( F% + L% ) \ 2 '3 mid way
IF M% = F% THEN EXIT LOOP '3 bingo!
IF Srch$ =< A$(M%) THEN L% = M% ELSE F% = M% '3 which 1/2?
LOOP '3
'3
IF Srch$ > A$(M%) THEN M% = L% '3 past it!
fBinSearch% = M% '3
'???????????????
END FUNCTION
Next, test if the user presses a key > CHR$(31) and < CHR$(255). If so, and
SrchLen% is less that whatever max you've set then add one to SrchLen%, add
the new char to Srch$ and send it to fBinSearch%. If the LEFT$ of the returned
element equals Srch$ then, you've got a match. If not then strip off the last
letter you've just added.
If the incoming key stroke is NOT a 'letter' then clear Srch$
IF ( C% > 31 ) AND ( C% < 256 ) THEN
Srch$ = Srch$ + CHR$(C%)
SrchLen% = SrchLen% + 1
P% = fBinSearch%( A$(), Last%, Srch$ )
IF LEFT$( A$(P%), SrchLen% ) <> Srch$ THEN
SrchLen% = SrchLen% -1
Srch$ = LEFT$( Srch$, SrchLen% )
END IF
ELSE
SrchLen% = 0
Srch$ = ""
' PROCESS OTHER KEY STROKES HERE
END IF
____ _ ____ ____ _____
| _ \ / \ / ___) __ | ___)(_ _)
| |_) / _ \ \____\/ \| _) | |
|____//_/ \_\(____/\__/|_| |_|
www.basicguru.com/schullian