Arrow menues

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Arrow menues

Post by izidor »

Why sometimes one key press isn't enough?

Code: Select all

locate 1,4
print "goo"
locate 2,4
print "goo"
locate 1,1
print ">"
do
    if INKEY$ = chr$(0) + chr$(72) then cursor = 1
    if INKEY$ = chr$(0) + chr$(80) then cursor = 2

if cursor = 1 then
locate 1,1
print ">"
locate 2,1
print " "

elseif cursor = 2 then

locate 2,1
print ">"
locate 1,1
print " "
elseif INKEY$ = chr$(0) + chr$(72) AND cursor = 1 then cursor = 1
elseif INKEY$ = chr$(0) + chr$(80) AND cursor = 2 then cursor = 2
end if
loop
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Because you use INKEY$ many times. Eliminate the 2 ElseIF's. That is just repetition.

To best read one press, use an INKEY$ variable. Like K$ = INKEY$ at start of loop. Then use the variable in the 2 keypress statements. INKEY$ only holds a keypress until it is read once!
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor »

Yes! That's exactly what I wanted!
Thanks!

And btw Q-Basics is great!
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Thanks. But it seems like it will never be done! I keep finding more to add.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor »

Code: Select all

locate 1,4
print "goo"
locate 2,4
print "goo"
locate 1,1
print ">"

do
K$ = INKEY$
    if K$ = chr$(0) + chr$(72) then cursor = 1
    if K$ = chr$(0) + chr$(80) then cursor = 2

if cursor = 1 then
locate 1,1
print ">"
locate 2,1
print " "

elseif cursor = 2 then

locate 2,1
print ">"
locate 1,1
print " "

end if
loop
How to make more options in the menu?
User avatar
DDastardly71
Coder
Posts: 22
Joined: Fri Oct 17, 2008 6:56 pm
Location: U.S.A.

Post by DDastardly71 »

Here is a simple way to dynamically add or delete menu items. This code only looks long because it's indended and its in blocks.

Code: Select all

'
' goo1
'

DEFINT A-Z

Main:
  CLS
  
  '-- To add or delete items from the array you only need to
  '   modify the data statement and do two things...
  '  
  '   1. Change the first data statement to the number of
  '      elements in the array.
  '
  '   2. Add the row, col, and text anywhere below the first
  '      data statement.
  '
  '
  ' ****************************************************************
  '   NOTE: NO NEED TO MODIFY THIS CODE, JUST THE DATA STATEMENTS.
  ' ****************************************************************

  RESTORE
  READ NumItems

  REDIM Item$(1 TO NumItems), Row(1 TO NumItems), Col(1 TO NumItems)

  FOR i = 1 TO NumItems
    READ Row(i), Col(i), Item$(i)

    LOCATE Row(i), Col(i)
    PRINT Item$(i)
  NEXT

  '--

  LastChoice = 0
  Choice = 1

  DO
    IF LastChoice <> Choice THEN

      '-- This removes the previous pointer
      IF LastChoice > 0 THEN
        LOCATE LastChoice, 8: PRINT " "
      END IF
           
      LOCATE Choice, 8: PRINT ">"
      LastChoice = Choice
    END IF

    '--

    SELECT CASE INKEY$
      CASE CHR$(27)                       'escape
        Choice = 0
        EXIT DO
      CASE CHR$(13)                       'enter
        EXIT DO
      CASE CHR$(0) + CHR$(71)             'home
        Choice = 1
      CASE CHR$(0) + CHR$(79)             'end
        Choice = NumItems
      CASE CHR$(0) + CHR$(72)             'up
        IF Choice > 1 THEN
          Choice = Choice - 1
        ELSE
          Choice = NumItems
        END IF
      CASE CHR$(0) + CHR$(80)             'down
        IF Choice < NumItems THEN
          Choice = Choice + 1
        ELSE
          Choice = 1
        END IF
    END SELECT
  LOOP

  '-- 

  CLS

  IF Choice > 0 THEN
    PRINT "You selected item"; Choice
  ELSE
    PRINT "You made no selection!"
  END IF
END

DATA 5                                   
DATA 1, 10, "orange"                     
DATA 2, 10, "pear"                       
DATA 3, 10, "apple"                      
DATA 4, 10, "peach"                      
DATA 5, 10, "banana"                     
izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor »

Wow, that's great! Thank you! :D

EDIT: I know how to edit column but how to change row of ">" ?
User avatar
DDastardly71
Coder
Posts: 22
Joined: Fri Oct 17, 2008 6:56 pm
Location: U.S.A.

Post by DDastardly71 »

For this example, the first time is in row 1, therefore, I only had to use Choice% as the pointer for the actual row.

LOCATE Choice, 8: PRINT ">"
LastChoice = Choice

However, if you wanted to move the menu items to a different position, change the data statement to reflect the new position..

Say we want to modify the current DATA statement which starts at row=1 col=10 to row=5 col10

DATA 5
DATA 1, 10, "orange"
DATA 2, 10, "pear"
DATA 3, 10, "apple"
DATA 4, 10, "peach"
DATA 5, 10, "banana"

Now we want to put these items in the center of the screen...

DATA 5
DATA 8, 10, "orange"
DATA 9, 10, "pear"
DATA 10, 10, "apple"
DATA 11, 10, "peach"
DATA 12, 10, "banana"

The part of the code that you need change is this.

Change from this...

LOCATE Choice, 8: PRINT ">"
LastChoice = Choice

To this...

LOCATE Row%(Choice), Col%(Choice) - 2: PRINT ">"
LastChoice = Choice

I hope this helps.
Post Reply