BBC BASIC for Windows
Programming >> User Interface >> Combobox: removing last blank entries
http://bb4w.conforums.com/index.cgi?board=ui&action=display&num=1405202266

Combobox: removing last blank entries
Post by g3nrw on Jul 12th, 2014, 9:57pm

I have a combobox where I dynamically change the list items. Whenever I want to update the list, I first clear the existing list with:

SYS "SendDlgItemMessage", !dlg%, comboBox%, CB_RESETCONTENT

then repopulate the list with multiple CB_ADDSTRING calls.

This works fine if the new list contains exactly the same number of items as the old list (or more items than the old list), but if the new list has fewer items than the old, the new list then contains one or more blank entries at the end.

For example, if old list contains 4 entries:
~~~~~~~~~~~~~~~~~~~~~~~~
A
B
C
D
~~~~~~~~~~~~~~~~~~~~~~~~

and new list contains just 2 entries:
~~~~~~~~~~~~~~~~~~~~~~~~
E
F
~~~~~~~~~~~~~~~~~~~~~~~~

the new list is actually diplayed as:
~~~~~~~~~~~~~~~~~~~~~~~~
E
F
<blank entry>
<blank entry>
~~~~~~~~~~~~~~~~~~~~~~~~

By themselves, the blank entries don't do any harm, but they don't look good, and can confuse the user.

How do I remove these blank entries? I have Googled for hours for a solution without success!

--
Ian




Re: Combobox: removing last blank entries
Post by rtr on Jul 13th, 2014, 06:43am

on Jul 12th, 2014, 9:57pm, g3nrw wrote:
if the new list has fewer items than the old, the new list then contains one or more blank entries at the end.

I cannot reproduce that effect. I modified DLGDEMO.BBC (supplied with BB4W) by adding to the combobox initialisation code as follows:

Code:
      SYS "SendDlgItemMessage", !dlg%, 103, CB_ADDSTRING, 0, "Combobox 1"
      SYS "SendDlgItemMessage", !dlg%, 103, CB_ADDSTRING, 0, "Combobox 2"
      SYS "SendDlgItemMessage", !dlg%, 103, CB_ADDSTRING, 0, "Combobox 3"
      SYS "SendDlgItemMessage", !dlg%, 103, CB_ADDSTRING, 0, "Combobox 4"
      SYS "SendDlgItemMessage", !dlg%, 103, CB_RESETCONTENT, 0, 0
      SYS "SendDlgItemMessage", !dlg%, 103, CB_ADDSTRING, 0, "Combobox 1"
      SYS "SendDlgItemMessage", !dlg%, 103, CB_ADDSTRING, 0, "Combobox 2" 

The resulting combobox has only two items in its list box, with no blank entries (Windows 8.1).

Richard.
Re: Combobox: removing last blank entries
Post by g3nrw on Jul 13th, 2014, 8:22pm

Richard

That piece of code works as expected. However, the code below is closer to my app.

I use the EVENT library to detect a click on the combobox. When you click on the box, the code deletes the 4 old item values and displays 2 new values, plus the 2 blank entries I am complaining about. If you click on the combobox a second time, only the 2 new entries are displayed:

Code:
      REM. Modified Program to demonstrate combobox event handling
      INSTALL @lib$+"WINLIB"

      INSTALL @lib$+"WINLIB2"
      INSTALL @lib$+"WINLIB3"

      INSTALL @lib$+"WINLIB5"
      INSTALL @lib$+"WINLIB5A"
      INSTALL @lib$+"NOWAIT"
      INSTALL @lib$+"EVENTLIB"

      CBN_DROPDOWN = 7

      BS_DEFPUSHBUTTON = &1
      CB_ADDSTRING = &143
      CB_SETCURSEL = &14E
      CB_RESETCONTENT = &14B
      CBS_DROPDOWNLIST = &3
      ES_AUTOHSCROLL = &80
      ES_NUMBER = &2000
      LB_ADDSTRING = &180
      LB_GETCURSEL = &188
      UDM_SETRANGE = &465
      UDS_ALIGNRIGHT = &4
      UDS_AUTOBUDDY = &10
      UDS_SETBUDDYINT = &2
      WS_CHILD = &40000000
      WS_GROUP = &20000
      WS_VISIBLE = &10000000
      WM_COMMAND = 273

      dlg%=FN_newdialog("Dialogue box", 20, 20, 160, 128, 8, 560)
      PROC_groupbox(dlg%, "Group box", 0, 4, 4, 152, 96, WS_GROUP)

      PROC_editbox(dlg%, "Text box", 101, 12, 20, 64, 12, ES_AUTOHSCROLL)
      PROC_editbox(dlg%, "123456", 102, 82, 20, 64, 12, ES_NUMBER)
      PROC_dlgctrl(dlg%, "", 109, 0, 0, 12, 12, WS_VISIBLE OR WS_CHILD OR \
      \ UDS_AUTOBUDDY OR UDS_ALIGNRIGHT OR UDS_SETBUDDYINT, "msctls_updown32")

      PROC_combobox(dlg%, "", 103, 12, 40, 64, 60, CBS_DROPDOWNLIST)
      PROC_listbox(dlg%, "", 104, 82, 40, 64, 48, 0)

      PROC_radiobutton(dlg%, "Radiobutton 1", 105, 12, 64, 64, 10, 0)
      PROC_radiobutton(dlg%, "Radiobutton 2", 106, 12, 82, 64, 10, 0)
      PROC_checkbox(dlg%, "Checkbox", 107, 82, 82, 64, 10, 0)

      PROC_pushbutton(dlg%, "OK", 1, 12, 108, 56, 14, WS_GROUP OR BS_DEFPUSHBUTTON)
      PROC_pushbutton(dlg%, "Cancel", 2, 92, 108, 56, 14, 0)

      PROC_showdialog(dlg%)
      ON CLOSE PROC_closedialog(dlg%):QUIT
      ON ERROR PROC_closedialog(dlg%):PRINT'REPORT$:END

      SYS "SendDlgItemMessage", !dlg%, 103, CB_ADDSTRING, 0, "Combobox 1"
      SYS "SendDlgItemMessage", !dlg%, 103, CB_ADDSTRING, 0, "Combobox 2"
      SYS "SendDlgItemMessage", !dlg%, 103, CB_ADDSTRING, 0, "Combobox 3"
      SYS "SendDlgItemMessage", !dlg%, 103, CB_ADDSTRING, 0, "Combobox 4"
      SYS "SendDlgItemMessage", !dlg%, 103, CB_SETCURSEL, 0, 0

      SYS "SendDlgItemMessage", !dlg%, 104, LB_ADDSTRING, 0, "Listbox item 0"
      SYS "SendDlgItemMessage", !dlg%, 104, LB_ADDSTRING, 0, "Listbox item 1"
      SYS "SendDlgItemMessage", !dlg%, 104, LB_ADDSTRING, 0, "Listbox item 2"
      SYS "SendDlgItemMessage", !dlg%, 104, LB_ADDSTRING, 0, "Listbox item 3"

      SYS "CheckRadioButton", !dlg%, 105, 106, 105
      SYS "SendDlgItemMessage", !dlg%, 109, UDM_SETRANGE, 0, 999


      PROC_eventinit

      PROC_eventregister(WM_COMMAND, PROCcontrolclick())

      REPEAT
        PROC_eventpoll
        WAIT 1
      UNTIL FALSE

      DEF PROCcontrolclick(M%, W%, L%)
      LOCAL controlID%, notificationCode%

      controlID% = W% AND &FFFF
      notificationCode% = W% >> 16

      CASE controlID% OF
        WHEN 103 :
          IF notificationCode% = CBN_DROPDOWN THEN
            SYS "SendDlgItemMessage", !dlg%, 103, CB_RESETCONTENT, 0, 0
            SYS "SendDlgItemMessage", !dlg%, 103, CB_ADDSTRING, 0, "Combobox 5"
            SYS "SendDlgItemMessage", !dlg%, 103, CB_ADDSTRING, 0, "Combobox 6"
            SYS "SendDlgItemMessage", !dlg%, 103, CB_SETCURSEL, 0, 0
          ENDIF
      ENDCASE
      ENDPROC


      END
 



So why does it display the 2 blank entries when you click on the box the first time, but no blank entries on subsequent clicks?

--
Ian



Re: Combobox: removing last blank entries
Post by rtr on Jul 13th, 2014, 9:46pm

on Jul 13th, 2014, 8:22pm, g3nrw wrote:
So why does it display the 2 blank entries when you click on the box the first time, but no blank entries on subsequent clicks?

Presumably because you're updating the contents whilst it's dropped down. The combobox has something of a dilemma if you try to reduce the number of items from 4 to 2 whilst the listbox is displayed; it could choose to dynamically resize the box, or it could choose to blank the now 'unused' (but still displayed) lines. It seems that it uses the second strategy, probably because that minimises undesirable side effects.

If it worries you, update the combobox whilst it's not dropped down. Alternatively first close up the list, update its contents, and then drop it down again (but that will require some care to avoid repeatedly triggering the CBN_DROPDOWN event).

I find it hard to imagine in what circumstances it would be appropriate to change the contents of a combobox as a result of the user clicking on it, as you are doing. Surely if a user action changes the number of items, that action must be associated with a different control?

Richard.


Re: Combobox: removing last blank entries
Post by rtr on Jul 18th, 2014, 10:46pm

on Jul 13th, 2014, 9:46pm, Richard Russell wrote:
Surely if a user action changes the number of items, that action must be associated with a different control?

Perhaps I can clarify by means of an example. Consider the supplied program DLGDEMO.BBC, which displays a dialogue box containing two edit controls (one numeric), a combobox, a listbox, a checkbox and a pair of radio buttons.

Suppose that the combobox displays the contents of a disk directory, whilst the other controls modify what information is included. It might work something like this:
I'm sure you can see where this is leading. The one control which you can be confident does not affect the contents of the combobox is the combobox itself! So the issue which is the subject of this thread shouldn't arise.

Richard.
Re: Combobox: removing last blank entries
Post by g3nrw on Jul 20th, 2014, 10:22am

Quote:
I'm sure you can see where this is leading.
Richard.


Yes indeed I can.

I have restructured my program so that it now polls for new external conditions once per second, and only updates the combox if any previous conditions have changed. Everything now works as wanted.

Thanks for all the feedback.

--
Ian