home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / ARAYFUNC.ZIP / ARAYFUNC.PRG
Encoding:
Text File  |  1988-03-15  |  3.1 KB  |  94 lines

  1. * This is a sample piece of code which demonstrates how to make
  2. * an ACHOICE() menu act exactly like a MENU TO menu. It involves
  3. * creating a User Defined Function to simulate first letter
  4. * selection and wrap.
  5. *
  6. * First I declare the array for the menu,  I then fill the array
  7. * with my menu choices making sure that the first letter I want
  8. * to select on is the first character in each array.
  9. *
  10. * The reason for this is because I find ACHOICE preferable to
  11. * MENU TO for menus due to it's ability to access a UDF and do
  12. * other things while remaining in the menu.  It is possible to
  13. * write large applications in Clipper in which the whole program
  14. * is executed from within an ACHOICE menu.  In this example I also
  15. * illustrate a method of spreading out the menu choices.
  16.  
  17. PUBLIC array[10],logic[10]
  18. afill(logic,.f.)
  19. afill(array,space(5))
  20. for b=1 to 5
  21.   array[b*2] = ltrim(str(b*10000))
  22.   logic[b*2] = .t.
  23. next
  24.  
  25. * Now I set up the menu and put it in a loop for easy testing.
  26. set color to w/n,n/w,,,w/n
  27. clear
  28. @ 5,32 to 17,48 double
  29. do while LASTKEY() # 27
  30.   answer = Achoice(6,35,16,45,array,logic,"arrayfunc")/2
  31.   @ 23,38 say answer
  32. enddo
  33. return
  34.  
  35. * Here is the UDF that makes this happen.  First I get the parameters
  36. * that Clipper passes.  The only one I care about is MODE, but I get
  37. * the rest for good measure.  Then I capture the LASTKEY() for later
  38. * use. Now on to the good stuff.
  39.  
  40. FUNCTION arrayfunc
  41.   parameter mode, element, position
  42.   last1 = lastkey()
  43.  
  44.   * If mode = 1, they hit the up arrow at the top of the list, I
  45.   * keyboard a Ctrl-PgDn to put the cursor at the bottom of the list,
  46.   * and if mode = 2, they hit down at the bottom of the list, I
  47.   * keyboard a Ctrl-Home to put them at the top of the list.
  48.   do case
  49.  
  50.     case mode = 1
  51.        keyboard chr(30)
  52.        return 2
  53.  
  54.     case mode = 2
  55.        keyboard chr(29)
  56.        return 2
  57.  
  58.     * If mode = 0, they hit a legal movement key, I return a 2 which says
  59.     * keep going with the selection process.
  60.  
  61.     case mode = 0
  62.        return 2
  63.  
  64.     * If mode = 3, they hit a key which is not internally processed then
  65.     * I need to see if it is a key that I should act on. For Esc, 27, I
  66.     * return a 0 which aborts achoice and returns a 0.  For Enter, 13, I
  67.     * return a 1 which says to abort achoice and return the number of the
  68.     * item that is currently selected.  If they hit a key which corresponds
  69.     * to the first letter of one of the selections, tested with ascan()>0,
  70.     * I keyboard an Enter so that the next time around the current
  71.     * selection is made.  Last  I return a 3 which tells ACHOICE to
  72.     * move the cursor to the first selection which coresponds to the
  73.     * last key hit.
  74.  
  75.     case mode = 3
  76.        if LAST1 = 27
  77.           return 0
  78.        elseif last1 = 13
  79.           return 1
  80.        elseif ascan(array,upper(chr(last1)))>0
  81.           found1 = .T.
  82.           keyboard chr(13)
  83.        endif
  84.        return 3
  85.  
  86.   endcase
  87.  
  88.   * If nothing worked then I return a 2 which says to continue
  89.   * the selection process.
  90.  
  91.   return 2
  92.  
  93. *ENDFUNC arrayfunc
  94.