Author Topic: Add/Remove item from a Listbox  (Read 8879 times)

0 Members and 1 Guest are viewing this topic.

Matthew H

  • Newt
  • Posts: 70
Add/Remove item from a Listbox
« on: February 17, 2020, 04:54:50 PM »
I have two Listbox's.
http://help.autodesk.com/view/ACD/2020/ENU/?guid=GUID-C01592FC-3A6B-48A2-952C-15307F0DAC5F
Listbox1 never changes and has (Item A, Item B, Item C, Item D, Item E, ect)
The Listbox2 is empty.

Is there a way to select(single and multiple) items from Listbox1 to add to Listbox2?
As well as remove(single and multiple) items from Listbox2.
The finished list of Listbox2 would like similar to (Item B, Item B, Item B, Item E, Item C)

I notice Lee Mac has the Listbox function to reorder items in a Listbox, which is useful
http://www.lee-mac.com/listboxfunctions.html

I have attached an example below.

Thank you for your time,
Matthew H.

Lee Mac

  • Seagull
  • Posts: 12904
  • London, England
Re: Add/Remove item from a Listbox
« Reply #1 on: February 17, 2020, 06:13:37 PM »
Here's a quick example for you to consider:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / *error* asc dch dcl des lst sel )
  2.  
  3.     (defun *error* ( msg )
  4.         (if (= 'int  (type dch)) (unload_dialog dch))
  5.         (if (= 'file (type des)) (close des))
  6.         (if (and (= 'str  (type dcl)) (findfile dcl)) (vl-file-delete dcl))
  7.         (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
  8.             (princ (strcat "\nError: " msg))
  9.         )
  10.         (princ)
  11.     )
  12.  
  13.     (if
  14.         (and
  15.             (setq dcl (vl-filename-mktemp nil nil ".dcl"))
  16.             (setq des (open dcl "w"))
  17.             (foreach str
  18.                '(
  19.                     "lb : list_box"
  20.                     "{"
  21.                     "    multiple_select = true;"
  22.                     "    fixed_width = true;"
  23.                     "    fixed_height = true;"
  24.                     "    width = 30;"
  25.                     "    height = 15;"
  26.                     "}"
  27.                     "bt : button"
  28.                     "{"
  29.                     "    fixed_width = true;"
  30.                     "    fixed_height = true;"
  31.                     "    width = 10;"
  32.                     "    height = 2;"
  33.                     "}"
  34.                     "test : dialog"
  35.                     "{"
  36.                     "    spacer_1;"
  37.                     "    : row"
  38.                     "    {"
  39.                     "        : lb { key = \"lb1\"; }"
  40.                     "        : column"
  41.                     "        {"
  42.                     "            fixed_height = true;"
  43.                     "            : bt { label = \"--->\"; key = \"add\"; }"
  44.                     "            : bt { label = \"<---\"; key = \"rem\"; }"
  45.                     "        }"
  46.                     "        : lb { key = \"lb2\"; }"
  47.                     "    }"
  48.                     "    spacer;"
  49.                     "    ok_only;"
  50.                     "    spacer;"
  51.                     "}"
  52.                 )
  53.                 (write-line str des)
  54.             )
  55.             (not (setq des (close des)))
  56.             (< 0 (setq dch (load_dialog dcl)))
  57.             (new_dialog "test" dch)
  58.         )
  59.         (progn
  60.             (setq asc 65)
  61.             (repeat 10 (setq lst (cons (strcat "Item " (chr asc)) lst) asc (1+ asc)))
  62.             (setq lst (reverse lst))
  63.            
  64.             (dclist "lb1" lst)
  65.  
  66.             (action_tile "add"
  67.                 (vl-prin1-to-string
  68.                    '(mapcar 'dclist '("lb1" "lb2")
  69.                         (mapcar 'set '(lst sel)
  70.                             (shiftitems (read (strcat "(" (get_tile "lb1") ")")) lst sel)
  71.                         )
  72.                     )
  73.                 )
  74.             )
  75.  
  76.             (action_tile "rem"
  77.                 (vl-prin1-to-string
  78.                    '(mapcar 'dclist '("lb2" "lb1")
  79.                         (mapcar 'set '(sel lst)
  80.                             (shiftitems  (read (strcat "(" (get_tile "lb2") ")")) sel lst)
  81.                         )
  82.                     )
  83.                 )
  84.             )
  85.            
  86.             (start_dialog)
  87.         )
  88.     )
  89.     (*error* nil)
  90.     (princ)
  91. )
  92.  
  93. (defun dclist ( key lst )
  94.     (start_list key)
  95.     (foreach itm lst (add_list itm))
  96.     (end_list)
  97. )
  98.  
  99. (defun shiftitems ( idx lb1 lb2 / int )
  100.     (setq int -1
  101.           lb2 (reverse lb2)
  102.           lb1 (vl-remove-if '(lambda ( x ) (if (member (setq int (1+ int)) idx) (setq lb2 (cons x lb2)))) lb1)
  103.     )
  104.     (list lb1 (reverse lb2))
  105. )
  106.