Using multiple selections in the listbox

Discussions related to the code libraries supplied with BB4W & BBCSDL
svein
Posts: 58
Joined: Tue 03 Apr 2018, 19:34

Using multiple selections in the listbox

Post by svein »

I am trying to use multiple selections in the listbox, but all i get is the last one selected.
What am i doing wrong ?
BBCsdl 1.33a

Code: Select all

      INSTALL @lib$ + "dlglib"

      REM Set colour palette:
      PROC_setdialogpalette

      REM Set font according to platform:
      IF INKEY(-256) = &57 THEN
        *FONT Segoe UI,10
      ELSE
        OSCLI "FONT """ + @lib$ + "DejaVuSans"",12"
      ENDIF

      REM Create the dialogue box 'template':
      dlg% = FN_newdialog("Dialogue box", 160, 140)
      DIM ListBox$(6)
      ListBox$() = "", "Listbox item 1", "Listbox item 2", "Listbox item 3", \
      \                "Listbox item 4", "Listbox item 5", "Listbox item 6"
      PROC_listbox(dlg%, "", 104, 82, 38, 64, 36, LBS_MULTIPLESEL)
      PROC_setlistboxarray(dlg%, 104, ListBox$(), DIM(ListBox$(),1))

      PROC_button(dlg%, "OK", 1, 12, 108, 56, 14, 0)
      result% = FN_showdialog(dlg%, &80000000, &80000000)

      CLS
      IF result% = 1 THEN
        First%=FN_getlistboxselect(dlg%,104)
        PRINT "First ";First%
        IF First%>0 THEN
          PROC_setlistboxselect(dlg%,104,First%)
          PRINT "Listbox selection was "; FN_getdlgitemtext(dlg%, 104)
          Next%=FN_getlistboxselectnext(dlg%,104,First%)
          PRINT "Next ";Next%
          WHILE Next%<>First%
            PROC_setlistboxselect(dlg%,104,Next%)
            PRINT "Listbox selection was "; FN_getdlgitemtext(dlg%, 104)
            Next%=FN_getlistboxselectnext(dlg%,104,Next%)
            PRINT "Next ";Next%
          ENDWHILE
        ELSE
          ListBox$ = FN_getdlgitemtext(dlg%, 104)
        ENDIF
      ENDIF
      END


Svein
Hated Moron

Re: Using multiple selections in the listbox

Post by Hated Moron »

svein wrote: Fri 20 Jan 2023, 20:46 I am trying to use multiple selections in the listbox, but all i get is the last one selected.
Your program is working as I would expect here (in Windows) but I'm unsure what the purpose of this code is:

Code: Select all

          PROC_setlistboxselect(dlg%,104,First%)
          PRINT "Listbox selection was "; FN_getdlgitemtext(dlg%, 104)
...
            PROC_setlistboxselect(dlg%,104,Next%)
            PRINT "Listbox selection was "; FN_getdlgitemtext(dlg%, 104)
I would have expected you to be simply printing the contents of the array:

Code: Select all

          PRINT "Listbox selection was "; ListBox$(First%)
...
            PRINT "Listbox selection was "; ListBox$(Next%)
You could do it indirectly by setting and then reading the selection - although I can't think of any benefit in doing so - but you'd have to refresh the dialogue box each time (as you always have to after making a 'programmatic' change):

Code: Select all

          PROC_setlistboxselect(dlg%,104,First%)
          PROC_refreshdialog(dlg%)
          PRINT "Listbox selection was "; FN_getdlgitemtext(dlg%, 104)
...
            PROC_setlistboxselect(dlg%,104,Next%)
            PROC_refreshdialog(dlg%)
            PRINT "Listbox selection was "; FN_getdlgitemtext(dlg%, 104)
With a complex dialogue box this could be quite slow, so I'd recommend the simple method of reading the array.
svein
Posts: 58
Joined: Tue 03 Apr 2018, 19:34

Re: Using multiple selections in the listbox

Post by svein »

Sigh, i overlooked the obviously simpler method of just reading the array.
Your answer also gave me the answer to an other "problem".
If the selected line is scrolled out of view, then FN_getdlgitemtext(dlg%, 104) returns an empty string.
One can simply use FN_getlistboxselect(dlg%,104) to get the selected line to pick from the array.
Thank you.
Svein
Hated Moron

Re: Using multiple selections in the listbox

Post by Hated Moron »

svein wrote: Sat 21 Jan 2023, 09:01 If the selected line is scrolled out of view, then FN_getdlgitemtext(dlg%, 104) returns an empty string.
Indeed that's true, I don't think I considered the possibility of somebody making a selection and then scrolling it out of view (if you make a selection 'programmatically' it is automatically scrolled into view)!

It's probably better not to use FN_getdlgitemtext() with a list box or combobox, but simply read from the array.