Board Logo
« Instant Menu Function »

Welcome Guest. Please Login or Register.
Jan 16th, 2018, 3:33pm


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Instant Menu Function  (Read 1038 times)
zzz000abc
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 268
xx Instant Menu Function
« Thread started on: Dec 2nd, 2016, 11:06am »

here is function for a text based menu in simple way
Code:
function menu$(task$,mask$)
[loop]
i=0
print "choose option:"
while word$(task$,i+1)<>""
 i=i+1:t$=word$(task$,i):m$=word$(mask$,i)
 if m$=""then mask$="":m$=str$(i)
   print m$;" - ";t$
wend
input "";op$

for j=1 to i
if op$=word$(mask$,j) or op$=str$(j) then exit for
next
if j=i+1 then cls :goto[loop]
if mask$<>"" then menu$=word$(mask$,j) else menu$=str$(j)
end function
 

here the function expects two strings
how to use.
example1.
st1="populate search minimum exit"
st2="P S M E"
mn$=menu$(st1,st2)
*note that the number of words in st1= the number of letters in st2
example2.
list2$="open edit copy append search close"
mn$=menu$(list2$,"")
*second string is optional in the absence of it use empty quotes ie.""
User IP Logged

tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3635
xx Re: Instant Menu Function
« Reply #1 on: Dec 2nd, 2016, 11:35am »

Nice small function.
Need to change parameters in first example to strings, though.
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
zzz000abc
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 268
xx Re: Instant Menu Function
« Reply #2 on: Dec 2nd, 2016, 9:54pm »

on Dec 2nd, 2016, 11:35am, tsh73 wrote:
Nice small function.
Need to change parameters in first example to strings, though.

* it was typing mistake corrected now.
here the function expects two strings
how to use.
example1.
st1$="populate search minimum exit"
st2$="P S M E"
mn$=menu$(st1$,st2$)
*note that the number of words in st1= the number of letters in st2
example2.
list2$="open edit copy append search close"
mn$=menu$(list2$,"")
**second string is optional in the absence of it use empty quotes ie.""
*** the function also needs little modification to handle both small and caps.
here is the modified code:
Code:
function menu$(task$,mask$)
[loop]
i=0
print "choose option:"
while word$(task$,i+1)<>""
 i=i+1:t$=word$(task$,i):m$=word$(mask$,i)
 if m$=""then mask$="":m$=str$(i)
   print m$;" - ";t$
wend
input "";op$

for j=1 to i
if upper$(op$)=upper$(word$(mask$,j)) or op$=str$(j) then exit for
next
if j=i+1 then cls :goto[loop]
if mask$<>"" then menu$=word$(mask$,j) else menu$=str$(j)
end function
 

User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: Instant Menu Function
« Reply #3 on: Dec 3rd, 2016, 08:02am »

I tested this with more tasks than masks and ran into some strange results, of course that's not suppose to happen.

Yes, I thought your examples did need case insensitive mask in the examples you provided but if you want to use A and a, you could have.

Have you considered just a task list, numbering the choices and then returning the task selected or it's number?
User IP Logged

B+
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: Instant Menu Function
« Reply #4 on: Dec 3rd, 2016, 1:57pm »

It is a good idea to have a menu function handy!

This turned out trickier than I expected to bullet proof.

;-{)) OK, I ran out of bullets, so you can fire at will...
Code:
'menu choice function.txt 2016-12-03

'using comma delimited menu list so more than 1 word may be used as list item
ttl$ = "" 'OK empty but OK
ttl$ = "Testing Menu Selection: a sample File menu"

'test for 0, 1 and many items
cMenu$ = ""    'OK
cMenu$ = "New" 'OK
cMenu$ = ",New,Open,Save,Save As,Print,Cancel"  'OK will see typo soon enough
cMenu$ = "New,Open,Save,Save As,Print,Cancel,"  'No problem
cMenu$ = "New,Open,Save,Save As,Print,Cancel"   'typical case
'do not put spaces after commas to separate items in string

do
    choice = nChoice(ttl$, cMenu$)
    print
    print "You chose number ";choice;" = Menu Item: ";word$(cMenu$, choice, ",")
    print : print "If you wish to cancel testing, press any letter and enter. Otherwise,"
    input "just press enter to continue testing menu selections... ";t$
    print
loop until len(t$)
print "Testing is done. Goodbye!"

function nChoice(atitle$, aMenu$)
' Displays a menu or list to select an item from a menu/list by
' entering it's number. This number is returned as nChoice
' from which word$(aMenu$, nChoice, ",") will retrieve the item string.
' Do not put spaces after commas to separate items in aMenu$

    if aMenu$ = "" then nChoice = 0 : exit function
    'EDIT: this was not needed wordcount = 0
    do
        scan
        w$ = word$(aMenu$, wordCount + 1, ",")
        if w$ <> "" then 'add to list and count
            wordCount = wordCount + 1
            if pp$ = "" then pp$ = wordCount;" ";w$ else pp$ = pp$ + chr$(13);wordCount;" ";w$
        end if
    loop until w$ = ""
    p$ = atitle$;chr$(13);pp$
    do
        scan
        print p$ : input "Enter the Integer of your choice > "; test  'EDIT from number to Integer
        if test <> int(test) then                                     'APPEND: this test for Integer
            notice test;" is not an Integer, so your choice is ambiguous."
            test = 0
        end if 
        if test > 0 and test <= wordCount then
            nChoice = test : exit function
        else
            if loopCount < 5 then
                print:print "Expecting an Integer between 1 and ";wordCount;"."  'EDIT: clearly want Integer not just any number
                print "If you wish to cancel, press any letter and enter."
                input "If you wish to continue just press enter >"; wayt$
                if len(wayt$) then nChoice = 0 : exit function
                print
            end if
        end if
        loopCount = loopCount + 1
    loop until loopCount > 5
    print "We seem to be stuck, will use no selection as your choice."
    nChoice = 0
end function

 


EDIT: removed
wordcount = 0 'it was supposed to be wordCount but not needed
as noted in code

EDIT 2 (2016-12-04): prompt for Integer not number of choice then notice when an Integer was not input (but a number was).
« Last Edit: Dec 4th, 2016, 10:29am by bplus » User IP Logged

B+
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: Instant Menu Function
« Reply #5 on: Dec 3rd, 2016, 4:31pm »

This might be handy, a list/menu dialog function:
Code:
'Select from list dialog.txt for JB v1.01 [B+=MGA] 2016-12-03
' make dialog for selecting from list or menu

'using comma delimited list so more than 1 word may be used as item.
ttl$ = "" 'OK empty but OK
ttl$ = "Testing Menu Selection: a sample File menu"

'test for 0, 1 and many items
cMenu$ = ""    'OK, but I did not cancel, there was nothing on list
cMenu$ = "New" 'OK
cMenu$ = ",New,Open,Save,Save As,Print,Exit"  'OK will see typo
cMenu$ = "New,Open,Save,Save As,Print,Exit,"  'No problem
cMenu$ = "New,Open,Save,Save As,Print,Exit"   'typical case

'ttl$ = "Testing Big List: Santa Selection"
cMenu$ = "Mark,Mary,Martin,Marge,Mike,Martha,Milton,Mildred,Max,"
cMenu$ = cMenu$;"Martinette,Marietta,Masked Man,Masked Woman,Many,"
cMenu$ = cMenu$;"More,Morely,Morsel,Mazelle,Mickey,Mighty,Master,"
cMenu$ = cMenu$;"Marlene,Maxine,Mini,Marlon,"
cMenu$ = cMenu$;"Mary Jane,Mary Joe,Mary Margaret,Merry Christmas!"
'do not put spaces after commas to separate items in string

do
    choice$ = selectDialog$(ttl$, cMenu$)
    print
    if choice$ = "" then
        print "You canceled."
    else
        print "You selected ";choice$
    end if
    print : print "If you wish to cancel testing,";
    print " press any letter and enter. Otherwise,"
    input "just press enter to continue testing menu selections... ";t$
    print
loop until len(t$)
print "Testing is done. Goodbye!"

function selectDialog$(atitle$, aMenu$)
' A modal dialog (can't do anything else until finish in dialog)
' intended to return the actual item selected from aMenu$

    if aMenu$ = "" then selectDialog$ = "" : exit function
    maxL = len(atitle$) 'APPEND so we can read it
    do 'count and measure words
        scan
        w$ = word$(aMenu$, wordCount + 1, ",")
        if w$ <> "" then wordCount = wordCount + 1
        if len(w$) > maxL then maxL = len(w$)
    loop until w$ = ""
    dim mList$(wordCount-1)
    for i = 1 to wordCount
        mList$(i-1) = word$(aMenu$, i, ",")
    next

    WindowWidth = maxL * 10 + 65
    if WindowWidth > 809 then WindowWidth = 809
    if WindowWidth < 200 then WindowWidth = 200
    ww = WindowWidth
    WindowHeight = 20 * wordCount + 50
    if WindowHeight > 532 then WindowHeight = 532
    if WindowHeight < 200 then WindowHeight = 200
    wh = WindowHeight
    UpperLeftX = 200
    UpperLeftY = 100
    listbox #sd.mList, mList$(), [sDSelected], 5, 7, ww-18, wh-40
    open atitle$ for dialog_nf_modal as #sd
    #sd "trapclose [selectDialogQuit]"
    #sd "font Arial 10 18";
    #sd.mList "singleclickselect"
    wait 'wait here for input event

[sDSelected]
    #sd.mList "selection? mSelected$"
    p$ = atitle$;chr$(13);"You selected ";mSelected$
    confirm p$;yes$
    if yes$ = "yes" then
        selectDialog$ = mSelected$  'this should fall right through
    else
        p$ = atitle$;chr$(13);"Do you wish to Cancel?"
        confirm p$;yes$
        if yes$ = "yes" then
            selectDialog$ = "" : close #sd : exit function
        end if
        wait 'wait for another event
    end if

[selectDialogQuit]
    close #sd
end function
 


I first attempted this with a prompt dialog but it seemed limited to one line.

Now someone (tsh73?) can tell how to do this with a prompt dialog. LOL (Oh well, it seems good practice anyway.)

APPEND (2016-12-03 7PM):
maxL = len(atitle$) 'APPEND so we can read it
noted in code above
« Last Edit: Dec 4th, 2016, 10:27am by bplus » User IP Logged

B+
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3635
xx Re: Instant Menu Function
« Reply #6 on: Dec 4th, 2016, 02:49am »

Quote:
OK, I ran out of bullets, so you can fire at will...

Quote:
Enter the number of your choice > 2.5

dies with
Quote:
Runtime error: system primitive failed

User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: Instant Menu Function
« Reply #7 on: Dec 4th, 2016, 10:12am »

:-)) Hey a decimal is like a bullet!

At first I was going to fix with just this:
Code:
if test <> int(test) then test = 0 


But I decided to teach "Integer" with this:
Code:
        print p$ : input "Enter the Integer of your choice > "; test  'EDIT from number to Integer
        if test <> int(test) then                                     'APPEND: this test for Integer
            notice test;" is not an Integer, so your choice is ambiguous."
            test = 0
        end if 


Code above edited 2016-12-04 11AM with fix for non integer choices.

Append: OK this line should also change number to Integer
Code:
 print:print "Expecting an Integer between 1 and ";wordCount;"." 'EDIT also! 

« Last Edit: Dec 4th, 2016, 10:34am by bplus » User IP Logged

B+
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls